I wrote a simple component like this:
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const HomeView(),
);
}
}
final data = signal(0);
void increment() {
data.value++;
}
class HomeView extends StatelessWidget {
const HomeView({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Demo')),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Watch((_) => Text(data.value.toString())),
const SizedBox(height: 20),
ElevatedButton(onPressed: increment, child: const Text('Increment')),
],
),
);
}
}
Then I edited this part:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Watch((_) => Text(data.value.toString())),
const SizedBox(height: 20),
ElevatedButton(onPressed: increment, child: const Text('Increment')),
],
),
),
);
}
I added a Center widget, and then I got an error:
FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)
Why is this happening? Did I write something wrong?