【问题标题】:Flutter call builder function from a normal function来自普通函数的 Flutter 调用构建器函数
【发布时间】:2021-01-04 03:17:36
【问题描述】:

我基本上想从视图中调用我的事件减速器。这是我的视图代码-

class HomeView extends StatelessWidget {

  /// When user pressed get data button
  getData() async {
    print("Entering Get Data");
    Builder(builder: (context) {
      print("Inside the builder");
      // ignore: close_sinks
      final homeEventReducer = BlocProvider.of<HomeEventReducer>(context);
      homeEventReducer.add(DataRequested());
      return Center(
        child: CircularProgressIndicator(),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Home'),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                new RaisedButton(
                  padding: const EdgeInsets.all(8.0),
                  textColor: Colors.white,
                  color: Colors.pink,
                  onPressed: getData,
                  child: new Text("Get Data"),
                ),
          ],
        ),
      ),
    );
  }
}

这就是我的HomeEventReducer 代码的样子 -

Stream<HomeState> _reduceDataRequestedEventToState(
      DataRequested event) async* {
    yield Loading();
    try {
      final data = await _useCase.getData();
      if (data != null) {
        yield DataReceivedStatus(message: data);
      } else {
        yield DataReceivedError(message: "Data Retrieval Error");
      }
    } catch (error) {
      yield DataReceivedError(message: error.message);
    }
  }

我有 2 个问题 -

  1. 在我的HomeView 内部,控制流没有进入Builder 内部,即我没有得到日志print("Inside the builder");

  2. HomeEventReducer 中的DataReceivedStatus 返回我需要在视图中访问的数据。我该怎么做?

我是 Flutter 的新手。任何帮助将不胜感激?

【问题讨论】:

    标签: android flutter dart bloc


    【解决方案1】:

    Builder 是一个小部件。当您将其构造函数称为Builder(... 时,您正在创建它的一个实例。您应该将该实例作为孩子提供给另一个小部件。你没有那样做。 builder 的那个实例会浪费,就像下面代码中的 sum 会浪费一样:

    1 + 1; // 2 is going to waste
    a = 1 + 1; // 2 is not going to waste
    

    我认为您希望在按下按钮时激活构建器。在这种情况下,您的代码可以是这样的:

      bool isDataRequested = false;
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('Home'),
          ),
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    isDataRequested != true ? new RaisedButton(
                      padding: const EdgeInsets.all(8.0),
                      textColor: Colors.white,
                      color: Colors.pink,
                      onPressed:() => setState(() => isDataRequested = true;),
                      child: new Text("Get Data"),
                    ) : Builder(
                      builder: // Place the builder code in here
                    ),
              ],
            ),
          ),
        );
      }
    
    

    一句友好的忠告:“我是 Flutter 的新手”和“我想使用 BLoC”这两个句子不能很好地结合在一起。我建议通过直接使用setState() 来使用更简单的状态管理方法,至少在您更好地掌握 Flutter 之前。

    【讨论】:

    • 非常感谢您的帮助,但 onPressed() 没有任何反应,即应该调用 HomeEventReducer 的构建器代码没有调用
    • CircularProgressIndicator 没有出现?
    • 按下“获取数据”按钮时,我希望调用构建器代码,但没有任何反应
    • 如果我能对我的问题回答是/否,我可以继续。你想要的东西有很多层,你必须一步一步去找到问题所在。
    • 好的...没有出现 CircularProgressIndicator
    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2012-01-28
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多