繁体   English   中英

Flutter - Providers 和 Future 调用,如何共享同一个实例?

[英]Flutter - Providers and Future calls, how to share the same instance?

我正在学习 Flutter 并且有些东西我无法掌握。 I implemented a Infinite scroll pagination, with a package ( infine_scroll_pagination ), it works fine, but the data this Package is getting, comes from a Future call, which takes data from the WEB , and parses it in my Provider Class .

我的问题是, Infinite Scroll widget加载的data无法在其state中的其他任何地方访问。

示例:让我们获取一个contact list ,一次加载 10 个联系人:

class ContactsBody extends StatefulWidget {
  @override
  _ContactsBodyState createState() => _ContactsBodyState();
}

class _ContactsBodyState extends State<ContactsBody> {
  static const _pageSize = 10;
  final PagingController<int, Contact> pagingController =
      PagingController(firstPageKey: 0);

  @override
  void initState() {
    super.initState();
    pagingController.addPageRequestListener((pageKey) {
      _fetchPage(pageKey);
    });
  }

  Future<void> _fetchPage(int pageKey) async {
    try {
      final newItems = await ContactsService().fetchContactsPaged(pageKey, _pageSize);
      final isLastPage = newItems.length < _pageSize;

      if (isLastPage) {
        pagingController.appendLastPage(newItems.contacts);
      } else {
        final nextPageKey = pageKey + 1;
        pagingController.appendPage(newItems.contacts, nextPageKey);
      }
    } catch (error) {
      pagingController.error = error;
    }
  }
  @override
  Widget build(BuildContext context) {
    return ContactsList(pagingController);
  }

  @override
  void dispose() {
    pagingController.dispose();
    super.dispose();
  }

所以基本上这个无限滚动 package 会一次获取我的联系人,10 个,这里是我的ContactsService调用:

 Future<Contacts> fetchContactsPaged(int pageKey, int pageSize) async {
    final response = await http.get(.....);

    if (response.statusCode == 200) {
    return Contacts.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Failed to load contacts');
    }
  }

最后,正如您在上面看到的,它使用其工厂方法“ fromJson() ”初始化我的Provider class (联系人),并返回parsed data

现在我的Provider class

class Contacts extends ChangeNotifier {
      List<Contact> _contacts = <Contact>[];
    
      Contacts();
    
      factory Contacts.fromJson(final Map<String, dynamic> json) {
        final Contacts contacts = Contacts();
        if (json['data'] != null) {
          json['data'].forEach((contact) {
            contacts.add(Contact.fromJson(contact));
          });
        }
        return contacts;
      }

  void add(final Contact contact) {
    this._contacts.add(contact);
    this.notifyListeners();
  }

我在这里遇到的问题是,当加载Inifinite Scroll listView时,例如我更改了单个联系人的state (例如,可以将联系人设置为收藏夹),

如何访问已初始化FUTURE调用的Contacts() classSAME instanc ,以便我可以访问该 ZA2F2ED4F8EBC2CBB4C21A29D40AB6 中数据的current state state?

当然,如果我将我的更改发布到POST ,并在我需要的地方重新获取新值,我将获得更新后的 state 数据,但我想了解如何在此处访问同一个实例并制作当前数据在应用程序内随处可用

编辑:我删除了原始答案,以便更好地了解 OP 想要实现的目标。

我在 GitHub 上做了一个回购,试图向您展示您想要实现的目标: https://github.com/Kobatsu/stackoverflow_66578191

您的代码中有一些令人困惑的事情:

  • 何时创建对象实例(ContactsService、Contacts)
  • 提供者使用
  • (访问 pagingController 的列表?)
  • 解析 JSON / 使用工厂方法

存储库产生以下结果: 在此处输入图像描述

当您更新列表(通过向下滚动)时,黄色容器会更新为联系人数量和收藏夹数量。 如果您单击联系人,它将成为收藏夹,并且黄色容器也会更新。

我评论了存储库以向您解释每个部分。

注意:您代码中的 Contacts class 在我的代码中变成了 ContactProvider。

ContactsService class 拨打 API 电话:

class ContactsService {
  static Future<List<Contact>> fetchContactsPaged(
      int pageKey, int pageSize) async {
    // Here, you should get your data from your API

    // final response = await http.get(.....);
    // if (response.statusCode == 200) {
    //   return Contacts.fromJson(jsonDecode(response.body));
    // } else {
    //   throw Exception('Failed to load contacts');
    // }

    // I didn't do the backend part, so here is an example
    // with what I understand you get from your API:
    var responseBody =
        "{\"data\":[{\"name\":\"John\", \"isFavorite\":false},{\"name\":\"Rose\", \"isFavorite\":false}]}";
    Map<String, dynamic> decoded = json.decode(responseBody);
    List<dynamic> contactsDynamic = decoded["data"];

    List<Contact> listOfContacts =
        contactsDynamic.map((c) => Contact.fromJson(c)).toList();

    // you can return listOfContacts, for this example, I will add 
    // more Contacts for the Pagination plugin since my json only has 2 contacts
    for (int i = pageKey + listOfContacts.length; i < pageKey + pageSize; i++) {
      listOfContacts.add(Contact(name: "Name $i"));
    }
    return listOfContacts;
  }
}

提供者的用法:

Consumer<ContactProvider>(
        builder: (_, foo, __) => Container(
              child: Text(
                  "${foo.contacts.length} contacts - ${foo.contacts.where((c) => c.isFavorite).length} favorites"),
              padding: EdgeInsets.symmetric(
                  horizontal: 20, vertical: 10),
              color: Colors.amber,
            )),
    Expanded(child: ContactsBody())
  ]),
)

ContactsBody class 中的 Fetch page 方法,我们将联系人添加到我们的 ContactProvider 中:

  Future<void> _fetchPage(int pageKey) async {
    try {
      // Note : no need to make a ContactsService, this can be a static method if you only need what's done in the fetchContactsPaged method
      final newItems =
          await ContactsService.fetchContactsPaged(pageKey, _pageSize);
      final isLastPage = newItems.length < _pageSize;
      if (isLastPage) {
        _pagingController.appendLastPage(newItems);
      } else {
        final nextPageKey = pageKey + newItems.length;
        _pagingController.appendPage(newItems, nextPageKey);
      }

      // Important : we add the contacts to our provider so we can get
      // them in other parts of our app
      context.read<ContactProvider>().addContacts(newItems);
    } catch (error) {
      print(error);
      _pagingController.error = error;
    }
  }

ContactItem 小部件,我们在其中更新收藏状态并通知听众:

class ContactItem extends StatefulWidget {
  final Contact contact;
  ContactItem({this.contact});

  @override
  _ContactItemState createState() => _ContactItemState();
}

class _ContactItemState extends State<ContactItem> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
        child: Padding(child: Row(children: [
          Expanded(child: Text(widget.contact.name)),
          if (widget.contact.isFavorite) Icon(Icons.favorite)
        ]), padding: EdgeInsets.symmetric(vertical: 8, horizontal: 10),),
        onTap: () {
          // the below code updates the item
          // BUT others parts of our app won't get updated because
          // we are not notifying the listeners of our ContactProvider !
          setState(() {
            widget.contact.isFavorite = !widget.contact.isFavorite;
          });

          // To update other parts, we need to use the provider
          context.read<ContactProvider>().notifyContactUpdated(widget.contact);
        });
  }
}

和 ContactProvider:

class ContactProvider extends ChangeNotifier {
  final List<Contact> _contacts = [];
  List<Contact> get contacts => _contacts;

  void addContacts(List<Contact> newContacts) {
    _contacts.addAll(newContacts);
    notifyListeners();
  }

  void notifyContactUpdated(Contact contact) {
    // You might want to update the contact in your database,
    // send it to your backend, etc...
    // Here we don't have these so we just notify our listeners :
    notifyListeners();
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM