Skip to content

Instantly share code, notes, and snippets.

@FantasyCheese
Created September 18, 2020 11:24
Show Gist options
  • Save FantasyCheese/6d6805524e16e5d5e3ed3da29abb8f10 to your computer and use it in GitHub Desktop.
Save FantasyCheese/6d6805524e16e5d5e3ed3da29abb8f10 to your computer and use it in GitHub Desktop.
Simplest Provider with ChangeNotifier Example
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(ChangeNotifierProvider(
create: (_) => Counter(),
child: MyApp(),
));
}
class Counter extends ChangeNotifier {
Counter() {
Timer.periodic(Duration(seconds: 1), (timer) {
_count++;
notifyListeners();
});
}
int _count = 42;
int get count => _count;
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RichText(
text: TextSpan(text: context.watch<Counter>().count.toString()),
textDirection: TextDirection.ltr,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment