小部件库捕获的颤动异常失败断言

时间:2018-06-07 04:47:13

标签: widget flutter flutter-layout

Flutter Widgets Text.dart

我得到的是异常。我只是在学习Flutter。

以下是我的代码:

import 'package:flutter/material.dart';

void main() => runApp(new FriendlyChatApp());

const String _name = "Hammad Tariq";

class FriendlyChatApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
  title: "Friendly Chat",
  home: new ChatScreen(),
);
}
}

class ChatScreen extends StatelessWidget {
ChatScreen({this.text});

final String text;

@override
Widget build(BuildContext context) {
return new Container(
  margin: const EdgeInsets.symmetric(vertical: 10.0),
  child: new Row(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
      new Container(
        margin: const EdgeInsets.only(right: 10.0),
        child: new CircleAvatar(
          child: new Text(_name),
        ),
      ),
      new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new Text(_name, style: Theme.of(context).textTheme.subhead),
          new Container(
            margin: const EdgeInsets.only(top: 5.0),
            child: new Text(text),
          ),
        ],
      )
    ],
  ),
 );
  /*return new Scaffold(
  appBar: AppBar(
    title: new Text("Chat App"),
  ),
  );*/
  }
 }

1 个答案:

答案 0 :(得分:0)

问题在于

child: new Text(text)

其中String text为空。

您已将text定义为可选参数,但未将其从FriendlyChatApp传递出去。

如果您需要父网页上的某些输入才能让孩子正常工作,那么您可以使用 @required 注释对其进行定义,因为您不会意外地错过它。