首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >flutter_bloc:生成函数返回null

flutter_bloc:生成函数返回null
EN

Stack Overflow用户
提问于 2020-06-07 22:27:22
回答 1查看 551关注 0票数 0

我正在用BLoC模式重写一个非常简单的应用程序。然而,当我尝试在我的应用程序中使用BLoC组件时,会出现一个错误。

我不知道怎么解决它,如果有人知道,我会非常高兴!

当我试图在BlocListener和BlocBuilder中构建我的小部件时,就会出现这个错误。

错误图像:

代码:

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../shared/custom_card.dart';

import '../bloc/average_bloc.dart';

class ArithmeticAverageScreen extends StatefulWidget {
  @override
  _ArithmeticAverageScreenState createState() => _ArithmeticAverageScreenState();
}


class _ArithmeticAverageScreenState extends State<ArithmeticAverageScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('arithmetic_average_title').tr(),
      ),
      body: BlocListener<AverageBloc, AverageState>(
        listener: (context, state) {
          if (state is AverageError) {
            return errorDialog(state.message);
          }
        },
        child: BlocBuilder<AverageBloc, AverageState>(
          builder: (context, state) {
            if (state is AverageInitial) {
              return buildListViewWithCards(context);
            }
          },
        ),
      ),
    );
  }
}

Widget errorDialog(message) {
  return AlertDialog(
    content: Text(message).tr(),
  );
}

Widget buildListViewWithCards(BuildContext context) {
  TextEditingController _textFieldController = TextEditingController();

  return Container(
    padding: EdgeInsets.all(20.0),
    child: ListView(
      children: <Widget>[
        CustomCard(
          child: Column(
            children: <Widget>[
              ListTile(
                leading: Icon(Icons.help),
                title: Text('arithmetic_average_help').tr(),
                subtitle: Text('arithmetic_average_help_content').tr(),
              )
            ],
          ),
        ),
        SizedBox(height: 16.0),
        CustomCard(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text('arithmetic_average_your_grades', style: Theme.of(context).textTheme.headline5).tr(),
                SizedBox(height: 16.0),
                Text('arithmetic_average_type_grades', style: Theme.of(context).textTheme.headline6).tr(),
                SizedBox(height: 16.0),
                Row(
                  children: <Widget>[
                    Container(
                      width: 60.0,
                      child: TextField(
                        controller: _textFieldController,
                        decoration: InputDecoration(
                          labelText: 'arithmetic_average_textfield_hint'.tr(),
                          hintText: '5'
                        ),
                      ),
                    ),
                    SizedBox(width: 16.0),
                    RaisedButton(
                      onPressed: () {
                        submitGrade(context, _textFieldController.text);
                      },
                      child: Text('arithmetic_average_add_button').tr(),
                      color: Colors.teal[300],
                      textColor: Colors.white,
                    )
                  ],
                )
              ],
            ),
          )
        ),
        SizedBox(height: 16.0),
        CustomCard(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text('arithmetic_average_your_average', style: Theme.of(context).textTheme.headline5).tr(),
                SizedBox(height: 16.0),
                Center(
                  child: Text('???', style: Theme.of(context).textTheme.headline4)
                )
              ],
            )
          )
        )
      ],
    )
  );
}

void submitGrade(BuildContext context, String average) {
  final averageBloc = BlocProvider.of<AverageBloc>(context);
  averageBloc.add(GetArithmeticAverage(average));
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-07 22:30:41

您没有在BlocBuilder中返回任何小部件,您只是调用了没有return的widget函数

代码语言:javascript
复制
class _ArithmeticAverageScreenState extends State<ArithmeticAverageScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('arithmetic_average_title').tr(),
      ),
      body: BlocListener<AverageBloc, AverageState>(
        listener: (context, state) {
          if (state is AverageError) {
            errorDialog(state.message);
          }
        },
        child: BlocBuilder<AverageBloc, AverageState>(
          builder: (context, state) {
            if (state is AverageInitial) {
              return buildListViewWithCards(context); // return the widget (you didn't add `return` before
            }else{
              return Container();
            }
          },
        ),
      ),
    );
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62252480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档