在库中的地图扑面而出执行提供商

发布于 2025-02-04 10:45:02 字数 1196 浏览 3 评论 0原文

我将我的提供商与FutureBuilder组件分离为库映射,以便可以将其用作动态和可重复使用的内容。这是看起来的简短版本:

page.dart(library)

library myproj;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '--path/to/my/provider'

Map<String, dynamic> firstPage = {
  'text': 'This is the first page',
  'action': <Map<String, dynamic>>[
    {
      'text': 'Next Page',
      'page': 2,
      'provider': (BuildContext context) =>
          Provider.of<Pages>(context, listen: false).checkNextPageCondition()
    }
  ]
};

component.dart(使用库的组件)

{
    ...,
    FutureBuilder(
      future: isolate-function(),
      initialData: {...},
      builder:(BuildContext context, AsyncSnapshot snapshot){
        return Column(
          children:[
            for(var el in snapshot.data['action']) TextButton(
              onPressed:(){
                el['provider'];
              },
              child: Text(el['text'])
            )
          ]
        )
      }
    )

}

这是大量编辑的代码,但是我是什么我正在尝试在这里实现,是将provider。我在提供商中定义的功能。但是,当我单击按钮时,什么也不会发生,并且在调试控制台中没有显示任何错误。

I am separating my Provider from FutureBuilder components into library Map so that I can use it as dynamic and reusable content. Here is the short version of how it look:

page.dart (library)

library myproj;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '--path/to/my/provider'

Map<String, dynamic> firstPage = {
  'text': 'This is the first page',
  'action': <Map<String, dynamic>>[
    {
      'text': 'Next Page',
      'page': 2,
      'provider': (BuildContext context) =>
          Provider.of<Pages>(context, listen: false).checkNextPageCondition()
    }
  ]
};

component.dart(component that use the library)

{
    ...,
    FutureBuilder(
      future: isolate-function(),
      initialData: {...},
      builder:(BuildContext context, AsyncSnapshot snapshot){
        return Column(
          children:[
            for(var el in snapshot.data['action']) TextButton(
              onPressed:(){
                el['provider'];
              },
              child: Text(el['text'])
            )
          ]
        )
      }
    )

}

This has been heavily redacted code, but what I am trying to achieve here, is to pass the Provider.of<Pages>(context, listen: false).checkNextPageCondition() to the TextButton onPressed so it can trigger the function that I have defined in my Provider. But when I click the button, nothing happened, and there isn't a single error showing up in the debug console.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2025-02-11 10:45:02

尝试一下

{
  'text': 'Next Page',
  'page': 2,
  'provider': Provider.of<Pages>(context, listen: false).checkNextPageCondition()
}

Try this

{
  'text': 'Next Page',
  'page': 2,
  'provider': Provider.of<Pages>(context, listen: false).checkNextPageCondition()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文