我无法在下一个 screen/route 访问新创建的提供程序
I can't access newly created provider on the next screen/route
Main() - MyApp 仅包含 ListItemWidget
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => MainProvider(),
),],
child: MyApp(),),
);
}
列表项目屏幕 - 是否允许为每个项目创建供应商?我想将项目索引作为提供者的参数传递。
class ListItemScreen extends StatelessWidget {
const ListItemScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final mainProvider = Provider.of<MainProvider>(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
),
body: ListView.builder(
itemCount: mainProvider.getItems().length,
itemBuilder: (context, index) {
final item = mainProvider.getItems()[index];
return ChangeNotifierProvider(
create: (_) => SecondProvider(secondItem: item),
builder: (context, child){
return ListTile(
leading: IconButton( // This onPressed can access the provider
onPressed: () { context.read<SecondProvider>().test();},
icon: Icon(Icons.add),
),
onTap: () { //When I press this and go to second it can't access it
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen()));
},
title: Text('TITLE TEST: ${mainProvider.getItems()[0].title}'),
);},);},),);}
}
SecondScreen
class SecondScreen extends StatelessWidget {
const SecondScreen({Key? key}) : super(key: key);
//It can't find the Provider (SecondProvder)
@override
Widget build(BuildContext context) {
final secondProvider = Provider.of<SecondProvider>(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
secondProvider.changeTitle();
},),
body: Center(
child: Text(
'Show Title: ${secondProvider.getTitle()}'
),));
}
}
SecondProvider
class SecondProvider extends ChangeNotifier{
MyItem _item;
SecondProvider({required MyItem item}) : _item = item;
void test(){
print('Test print ${_item.title}');
}
void changeTitle(){
_title = 'Test Title';
notifyListeners();
}
String getTitle() => _item.title;
}
基本上我希望 'item' 模型传递给我的提供者,以便我可以在下一个屏幕上访问它并通知视图更改,提供者将像视图模型一样工作。
您需要通过 SecondProvider
提供商
final _provider = Provider.of<SecondProvider>(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Provider.value(
value: _provider, child: SecondScreen())),
);
Main() - MyApp 仅包含 ListItemWidget
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => MainProvider(),
),],
child: MyApp(),),
);
}
列表项目屏幕 - 是否允许为每个项目创建供应商?我想将项目索引作为提供者的参数传递。
class ListItemScreen extends StatelessWidget {
const ListItemScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final mainProvider = Provider.of<MainProvider>(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
),
body: ListView.builder(
itemCount: mainProvider.getItems().length,
itemBuilder: (context, index) {
final item = mainProvider.getItems()[index];
return ChangeNotifierProvider(
create: (_) => SecondProvider(secondItem: item),
builder: (context, child){
return ListTile(
leading: IconButton( // This onPressed can access the provider
onPressed: () { context.read<SecondProvider>().test();},
icon: Icon(Icons.add),
),
onTap: () { //When I press this and go to second it can't access it
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen()));
},
title: Text('TITLE TEST: ${mainProvider.getItems()[0].title}'),
);},);},),);}
}
SecondScreen
class SecondScreen extends StatelessWidget {
const SecondScreen({Key? key}) : super(key: key);
//It can't find the Provider (SecondProvder)
@override
Widget build(BuildContext context) {
final secondProvider = Provider.of<SecondProvider>(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
secondProvider.changeTitle();
},),
body: Center(
child: Text(
'Show Title: ${secondProvider.getTitle()}'
),));
}
}
SecondProvider
class SecondProvider extends ChangeNotifier{
MyItem _item;
SecondProvider({required MyItem item}) : _item = item;
void test(){
print('Test print ${_item.title}');
}
void changeTitle(){
_title = 'Test Title';
notifyListeners();
}
String getTitle() => _item.title;
}
基本上我希望 'item' 模型传递给我的提供者,以便我可以在下一个屏幕上访问它并通知视图更改,提供者将像视图模型一样工作。
您需要通过 SecondProvider
提供商
final _provider = Provider.of<SecondProvider>(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Provider.value(
value: _provider, child: SecondScreen())),
);