Skip to content

BuildContext.select

BuildContext.select is a method that computes a value whenever the selected states change and registers the current widget as a listener for these changes. Consequently, each time the selected states change, the value is recalculated and if it is different from the previous one, the widget is automatically notified and rebuilt.

Syntax & description

V context.select<T, V>(
V computeValue(T instance, RtState select(RtState state)),
[String? id],
);
  • context: The BuildContext object, which provides information about the current widget in the tree.
  • T : The type of the dependency you want to select. It is used to locate the dependency from the nearest ancestor provider.
  • V : The type of the value you want to compute. It is the return type of the computeValue function.
  • computeValue : A function that computes the value based on the selected states. It takes two arguments:
    • instance: The instance of the dependency of type T .
    • select : A function that allows you to wrap the state( RtState ) to be listened for changes and returns it.
  • id: An optional identifier for the T dependency. If omitted, the dependency will be located by its type ( T ).

Usage

This following example demonstrates how to use BuildContext.select :

1
import 'package:flutter/widgets.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter_controller.dart';
4
5
class CounterDivisible extends StatelessWidget {
6
final int byNum;
7
8
const CounterDivisible({Key? key, required this.byNum}) : super(key: key);
9
10
@override
11
Widget build(BuildContext context) {
12
// Select the `count` state from the `CounterController`,
13
// calculate if the `count` is divisible by num(`byNum`)
14
// and rebuild the widget tree when the value(`isDivisibleByNum`) changes
15
final isDivisibleByNum = context.select<CounterController, bool>(
16
(counterController, $) => $(counterController.count).value % byNum == 0,
17
);
18
19
return Text(
20
isDivisibleByNum ? "Divisible by $byNum" : "Not divisible by $byNum",
21
);
22
}
23
}
1
import 'package:flutter/material.dart';
2
import 'counter.dart';
3
import 'counter_divisible.dart';
4
5
class CounterView extends StatelessWidget {
6
const CounterView({Key? key}) : super(key: key);
7
8
@override
9
Widget build(BuildContext context) {
10
return Scaffold(
11
appBar: AppBar(
12
title: const Text("Counter"),
13
),
14
body: Center(
15
child: Column(
16
mainAxisAlignment: MainAxisAlignment.center,
17
children: [
18
const Counter(),
19
SizedBox(height: 8),
20
const CounterDivisible(byNum: 2),
21
SizedBox(height: 8),
22
const CounterDivisible(byNum: 3),
23
SizedBox(height: 8),
24
const CounterDivisible(byNum: 5),
25
],
26
),
27
),
28
);
29
}
30
}
1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter_controller.dart';
4
5
class Counter extends StatelessWidget {
6
const Counter({Key? key}) : super(key: key);
7
8
@override
9
Widget build(BuildContext context) {
10
// Locale the `CounterController` dependency
11
return RtConsumer<CounterController>(
12
builder: (context, counterController, child) {
13
return Row(
14
mainAxisSize: MainAxisSize.min,
15
children: [
16
ElevatedButton(
17
onPressed: counterController.decrement,
18
child: const Icon(Icons.remove),
19
),
20
const SizedBox(width: 8),
21
// Observe the `count` property of the `counterController`
22
// and rebuild the widget tree when the `count` value changes
23
RtConsumer<CounterController>(
24
listenStates: (counterController) => [counterController.count],
25
builder: (context, counterController, child) {
26
return Text("${counterController.count}");
27
},
28
),
29
const SizedBox(width: 8),
30
ElevatedButton(
31
onPressed: counterController.increment,
32
child: const Icon(Icons.add),
33
),
34
],
35
);
36
},
37
);
38
}
39
}
1
import 'package:flutter/material.dart';
2
import 'package:flutter_reactter/flutter_reactter.dart';
3
import 'counter_view.dart';
4
import 'counter_controller.dart';
5
6
void main() {
7
runApp(MyApp());
8
}
9
10
class MyApp extends StatelessWidget {
11
@override
12
Widget build(BuildContext context) {
13
return MaterialApp(
14
// Provide the `CounterController` dependency to the widget tree
15
home: RtProvider.lazy(
16
() => CounterController(),
17
builder: (context, child) {
18
return CounterView();
19
},
20
),
21
);
22
}
23
}
1
import 'package:reactter/reactter.dart';
2
3
class CounterController {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

In this example, checks if the count state from CounterController is divisible by a specified number (byNum). The BuildContext.select method is used to perform this check and rebuild the widget tree whenever the divisibility status changes.