In this below code i made simply code to change Text widget string, default string can be shown into Text widget but when i try to click FloatingActionButton is doesn't change.
clicking on HotReload cause chage it to new value, where i should change to resolve this issue?
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/all.dart';
class MyString extends StateNotifier<String> {
MyString() : super('Hello World');
void change(String text) => state = text;
}
final showHello = StateNotifierProvider<MyString>((ref) => MyString());
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
appBar: AppBar(
title: Text('SAMPLE'),
),
body: Center(
child: Consumer(
builder: (context, watch, _) {
final s = watch(showHello).state;
return Text(s);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => context.read(showHello).change('Clicked On Button'),
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
),
);
}
}