gpt4 book ai didi

flutter - Flutter Provider嵌套导航

转载 作者:行者123 更新时间:2023-12-03 02:39:18 26 4
gpt4 key购买 nike

我在提供程序和导航上有问题。

我有一个带有对象列表的HomeScreen。当您单击一个对象时,我使用选项卡导航导航到DetailScreen。此DetailScreen包裹在ChangenotifierProvider中,后者提供了ViewModel
现在,当我使用Navigator.of(context).push(EditScreen)导航到另一个屏幕时,无法访问ViewModel中的EditScreen引发以下错误

════════ Exception caught by gesture ═══════════════════════════════════════════
The following ProviderNotFoundException was thrown while handling a gesture:
Error: Could not find the correct Provider<ViewModel> above this EditScreen Widget

这是我尝试实现的目标的简单概述
Home Screen
- Detail Screen (wrapped with ChangeNotifierProvider)
- Edit Screen
- access provider from here

我知道是什么问题。我正在堆栈上推一个新屏幕,并且更改通知器不再可用。
我考虑过要在我的应用程序顶部创建一个详细信息存储库,该存储库包含DetailView的所有ViewModel。

我知道我可以将ChangeNotifier包裹在我的MaterialApp周围,但是我不希望那样做,或者因为我不知道我需要哪个Detail-ViewModel而不能这样做。我想要列表中的每个项目都有一个ViewModel

我真的不知道什么是解决此问题的最佳方法。谢谢大家的帮助

这是一个快速的示例应用程序:

This is a picture of the image tree
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("DetailView"),
onPressed: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (_) => ViewModel(), child: DetailScreen()))),
)));
}
}

class DetailScreen extends StatelessWidget {
const DetailScreen({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("EditScreen"),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => EditScreen())),
),
));
}
}

class EditScreen extends StatelessWidget {
const EditScreen({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("Print"),
onPressed: () =>
Provider.of<ViewModel>(context, listen: false).printNumber()),
),
);
}
}

class ViewModel extends ChangeNotifier {
printNumber() {
print(2);
}
}

最佳答案

为了能够跨导航访问提供者,您需要在MaterialApp之前提供它,如下所示

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => ViewModel(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
),
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("DetailView"),
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DetailScreen(),
),
),
)));
}
}

class DetailScreen extends StatelessWidget {
const DetailScreen({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("EditScreen"),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => EditScreen())),
),
));
}
}

class EditScreen extends StatelessWidget {
const EditScreen({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("Print"),
onPressed: () =>
Provider.of<ViewModel>(context, listen: false).printNumber()),
),
);
}
}

class ViewModel extends ChangeNotifier {
printNumber() {
print(2);
}
}

关于flutter - Flutter Provider嵌套导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com