由于谷歌官方目前对provide停止了更新
现在采用的是Provider
首先导入包
import 'package:provider/provider.dart';
然后在Main函数中注册
void main(){
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_)=>Counter()),
ChangeNotifierProvider(create: (_)=>ChildCategory()),
ChangeNotifierProvider(create: (_)=>CategoryGoodsListProvider()),
ChangeNotifierProvider(create: (_)=>DetailsInfoProvider()),
ChangeNotifierProvider(create: (_)=>CartProvider())
],
child: MyApp(),
),
);
}
使用的时候会出现多种方式提供选择
第一种 使用 Provider.of 监听的地方记得把 listen设置为 true
List cartList = Provider.of
在发送数据的界面 记得把listen设置为false
比如:
InkWell(){
onTap(){
Provider.of
}
}
第二种方式 用 官方代码如下:
// ignore_for_file: public_member_api_docs, lines_longer_than_80_chars
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
/// This is a reimplementation of the default Flutter application using provider + [ChangeNotifier].
void main() {
runApp(
/// Providers are above [MyApp] instead of inside it, so that tests
/// can use [MyApp] while mocking the providers
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Counter()),
],
child: MyApp(),
),
);
}
/// Mix-in [DiagnosticableTreeMixin] to have access to [debugFillProperties] for the devtool
class Counter with ChangeNotifier, DiagnosticableTreeMixin {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
/// Makes `Counter` readable inside the devtools by listing all of its properties
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(IntProperty('count', count));
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Example'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('You have pushed the button this many times:'),
/// Extracted as a separate widget for performance optimization.
/// As a separate widget, it will rebuild independently from [MyHomePage].
///
/// This is totally optional (and rarely needed).
/// Similarly, we could also use [Consumer] or [Selector].
const Count(),
],
),
),
floatingActionButton: FloatingActionButton(
/// Calls `context.read` instead of `context.watch` so that it does not rebuild
/// when [Counter] changes.
onPressed: () => context.read().increment(),
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
class Count extends StatelessWidget {
const Count({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text(
/// Calls `context.watch` to make [MyHomePage] rebuild when [Counter] changes.
'${context.watch().count}',
style: Theme.of(context).textTheme.headline4);
}
}
上面官方的代码使用了
context.read
和
context.watch
一般read放到发送事件的地方比较好
watch一般用在接收数据的地方