在多个页面上保持提供者数据不起作用 [英] Persist Provider data across multiple pages not working

查看:98
本文介绍了在多个页面上保持提供者数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Flutter应用中使用了 Provider ,当我转到新页面时,提供给 Provider 在第2页上无法访问。

I'm using Provider in my flutter app, and when I go to a new page, the data provided to the Provider at page 1 is not accessible in page 2.

我理解 Provider 工作方式的方式,这是一个中央位置,一个位置可以存储所有数据,并且可以在应用程序中的任何位置访问该数据。因此,在我的应用程序(如下所示)中, ToDoListManager 是存储所有数据的地方。而且,如果我在页面1 中设置数据,那么我将能够访问页面2 中的数据,并且反之亦然。

The way I understood the way Provider works, was that there is a central place where one stores all the data, and one can access that data anywhere in the application. So in my application, which is shown below, ToDoListManager is the place where all the data is stored. And if I set the data in Page 1, then I will be able to access that data in Page 2, and vice versa.

如果这不正确,那么哪一部分是错误的呢?为何它在我的应用程序中不起作用?

If this is not correct, then what part is wrong? And why isn't it working in my application?

这是代码

第1页

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      builder: (context) => ToDoListManager(),
      child: Scaffold(
        appBar: AppBar(
          title: Text('Cool Project'),
        ),
        body:e ToDoList(),
      ),
    );
  }
}

class ToDoList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final toDoListManager = Provider.of<ToDoListManager>(context);

    return ListView.builder(
      itemCount: toDoListManager.toDoList.length,
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => Details(index)));
          },
          child: Text(toDoListManager.toDoList[index]),
        );
      },
    );
  }
}

第2页

class Details extends StatelessWidget {
  final int index;

  Details(this.index);

  @override
  build(BuildContext context) {
    return ChangeNotifierProvider(
      builder: (context) => ToDoListManager(),
      child: Scaffold(
          appBar: AppBar(
            title: Text('Details Bro'),
          ),
          body: AppBody(index)),
    );
  }
}

class AppBody extends StatelessWidget {
  final int index;

  AppBody(this.index);

  @override
  Widget build(BuildContext context) {
    final toDoListManager = Provider.of<ToDoListManager>(context);
    print(toDoListManager.toDoList);

    return Text(toDoListManager.toDoList[1]);
  }
}

ToDoListProvider

class ToDoListManager with ChangeNotifier {
  List<String> _toDoList = ['yo', 'bro'];

  List<String> get toDoList => _toDoList;

  set toDoList(List<String> newToDoList) {
    _toDoList = newToDoList;
    notifyListeners();
  }
}


推荐答案

您有2个选项:


  1. ChangeNotifierProvider 放在<$ c上方$ c> MaterialApp ,因此您可以通过任何 Navigator 路线访问。

  1. Place your ChangeNotifierProvider above your MaterialApp so that is accesible from any of you Navigator routes.

按原样保留您的 Home 小部件,但是当使用 Navigator 推送新的小部件时,请提供原始的Manager。 / p>

Keep your Home widget as is but when pushing the new widget with the Navigator provide the original Manager.



onTap: () {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) {
                return Provider<ToDoListManager>.value(
                    value: toDoListManager,
                    child: Details(index),
                );
            },
        ),
    );
},

使用这两种方法,您都无需创建新的 ChangeNotifierProvider 在您的详细信息屏幕中。

With both approaches you don't need to create a new ChangeNotifierProvider in your details screen.

这篇关于在多个页面上保持提供者数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆