簡體   English   中英

flutter_bloc:構建 function 返回 null

[英]flutter_bloc: Build function returned null

我正在使用 BLoC 模式在 Flutter 中重寫一個非常簡單的應用程序。 但是,當我嘗試在我的應用程序中使用 BLoC 組件時,會出現錯誤。

我不知道如何解決它,如果有人知道,我會非常高興!

當我嘗試在 BlocListener 和 BlocBuilder 中構建小部件時,會出現此錯誤。

錯誤圖片: 應用圖片

代碼:

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));
}

您沒有在BlocBuilder中返回任何小部件,您只是調用了 Widget 函數而沒有return

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();
            }
          },
        ),
      ),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM