简体   繁体   English

失败的断言:第 549 行 pos 12:“child.hasSize”:不正确

[英]Failed assertion: line 549 pos 12: 'child.hasSize': is not true

I am new to Flutter.我是 Flutter 的新手。 I am using bottom navigation view .我正在使用底部导航视图。 At the Dashboard class , there is the bottomnavigaton.Dashboard类中,有底部导航。 The first of bottom navigation is Home class which have the tab view.底部导航的第一个是具有选项卡视图的Home类。 The first page of the tabview is the NewRequest class. tabview 的第一页是NewRequest类。 When the application is first run, the first page of the tab view is not loaded but when going in other class and come back to the Home , the New Request is loaded.当应用程序第一次运行时,选项卡视图的第一页没有加载,但是当进入其他类并返回Home 时,加载了新请求 How to load the tabview first page when the app is first run?应用程序首次运行时如何加载tabview第一页? I have implemented as follows:我已实施如下:

class Dashboard extends StatefulWidget{

@override
 State<StatefulWidget> createState() {
   return _DashboardState();
 }

}
 class _DashboardState extends State<Dashboard>{

int _currentIndex=0;
bool home=true;
final tabs=[
 Home(),
 Center(child: Text('History')),
 Center(child: Text('Wallet')),
   Center(child: Text('More')),
  ];

 final _title=[
  Center(child: Text("Dashboard"),),
  Center(child:Text("History"),),
  Center(child:Text("Wallet"),),
  Center(child:Text("More"),),];


  @override
   Widget build(BuildContext context) {
    return Scaffold(
        appBar: home ? null :AppBar(
         title: _title[_currentIndex],
         automaticallyImplyLeading: false,
    ),
    body: Container(
    child: tabs[_currentIndex],
  ),
  bottomNavigationBar: _bottomNavigationBar(),
);
}

Widget _bottomNavigationBar(){
 return BottomNavigationBar(
    currentIndex: _currentIndex,
    type: BottomNavigationBarType.fixed,
    backgroundColor: Theme.of(context).primaryColor,
    selectedItemColor: Theme.of(context).accentColor,
    unselectedItemColor: Colors.white,
    items: [
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        title: Text("Home"),          
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.history),
        title: Text("History"),  
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.account_balance_wallet),
        title: Text("Wallet"), 
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.menu),
        title: Text("More"),
      )
    ],
    onTap: (index){
      setState(() {
        _currentIndex=index;
        if (index==0) {
          home=true;
        }else{
          home=false;
        }
      });
    },
  ) ;
 }
}

My Home page我的主页

class Home extends StatefulWidget {
  @override
   State<StatefulWidget> createState() {
    return _HomeState();
   }
}

 class _HomeState extends State<Home> {
  int index;
  @override
void initState() {
super.initState();
index = 0;
}

final pages = [
NewRequest(),
new Column(
  children: <Widget>[new Text("Accepted Page")],
),
new Column(
  children: <Widget>[new Text("Cooking Page")],
),
new Column(
  children: <Widget>[new Text("Cooked Page")],
)
];

final tabs = [
Text('Request'),
Text('Accepted'),
Text('Cooking'),
Text('Cooked'),
];

@override
Widget build(BuildContext context) {
return new MaterialApp(
  debugShowCheckedModeBanner: false,
  home: new DefaultTabController(
    length: 4,
    initialIndex: 0,
    child: new Scaffold(
        appBar: new PreferredSize(
          preferredSize: Size.fromHeight(56.0),
          child: new Container(
            color: Theme.of(context).primaryColor,
            child: new SafeArea(
              child: Column(
                children: <Widget>[
                  new Expanded(
                    child: new Container(
                        child: new TabBar(
                      unselectedLabelColor: Colors.white,
                      labelColor: Theme.of(context).accentColor,
                      indicatorColor: Theme.of(context).accentColor,
                      isScrollable: true,
                      tabs: List<Widget>.generate(4, (index) {
                        return tabs[index];
                      }),
                    )),
                  ),
                ],
              ),
            ),
          ),
        ),
        body: TabBarView(
          children: [
            NewRequest(),
            new Column(
              children: <Widget>[new Text("Accepted Page")],
            ),
            new Column(
              children: <Widget>[new Text("Cooking Page")],
            ),
            new Column(
              children: <Widget>[new Text("Cooked Page")],
            )
          ],
        )),
  ),
);
}

New Request page新请求页面

class NewRequest extends StatelessWidget{

@override
Widget build(BuildContext context) {

 return ListView(
    shrinkWrap: true,
    children: <Widget>[_CardView(), _CardView(), _CardView(), _CardView()],
  );    
 }  
}

 Widget _CardView() {
  return Card(
    margin: EdgeInsets.all(15.0),
    elevation: 15.0,
    child: Container(
      height: 185,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: FittedBox(
                  child: Image.asset('assets/images/momo.jpg'),
                  fit: BoxFit.fill,
                ),
              ),
              Container(
                  child: Expanded(
                      child: Container(
                margin: EdgeInsets.only(left: 10.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  mainAxisSize: MainAxisSize.max,
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        Text("Momo", style: TextStyle(fontSize: 16.0))
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          "Rs.100",
                          style: TextStyle(fontSize: 16.0),
                        )
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          "2 Plate",
                          style: TextStyle(fontSize: 16.0),
                        )
                      ],
                    ),
                  ],
                ),
              )))
            ],
          ),
          Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                Expanded(
                    child: Container(
                  margin: EdgeInsets.only(
                      left: 5.0, right: 5.0, top: 5.0, bottom: 2.0),
                  child: RaisedButton(
                    color: Colors.red,
                    child: Text(
                      "Reject",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () => null,
                  ),
                )),
                Expanded(
                    child: Container(
                  margin: EdgeInsets.only(
                      left: 5.0, right: 5.0, top: 5.0, bottom: 0.0),
                  child: RaisedButton(
                    color: Colors.green,
                    child: Text(
                      "Approve",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () => null,
                  ),
                )),
              ])
        ],
      ),
    ));
   }

This is late but I've just had the same issue.这已经很晚了,但我刚刚遇到了同样的问题。 I'm not sure how to solve your specific example but I'm posting my solution to my problem here in hopes that it might help someone else.我不确定如何解决您的具体示例,但我在这里发布了我的问题的解决方案,希望它可以帮助其他人。

return Row(children: [
    Container(child: Text('•'), margin: EdgeInsets.only(left: 25*indentLevel.toDouble(), right: 5),),
    Expanded(child: TextField(
      cursorColor: Colors.black,
      autofocus: true,
    ))
  ]);

My problem was with the TextField - Failed assertion: ... 'hasSize.'我的问题是TextField - Failed assertion: ... 'hasSize.'
The TextField doesn't have an explicit size option and instead inherits from its parent. TextField 没有明确的大小选项,而是从其父级继承。 (assumed from this post on sizing a TextField using a container.) If the parent also doesn't have a specific size (ex. Row, Column, ListView vertically) then errors can occur. (假设来自这篇关于使用容器调整 TextField 大小的帖子。)如果父级也没有特定的大小(例如行、列、ListView 垂直),则可能会发生错误。 As you can see in my code above, I was able to fix this issue by wrapping the TextField in an expanded Widget, which fills its parent to get its own dimensions which it in turn passes down to the TextField .正如您在上面的代码中看到的那样,我能够通过将TextField包装在一个展开的 Widget 中来解决这个问题,该 Widget 填充其父级以获得自己的尺寸,然后又向下传递给TextField

Widget which is throwing error can be wrapped in a Container with a Specified height.抛出错误的小部件可以包装在具有指定高度的容器中。 It worked for me.它对我有用。

@Nbn 尝试删除“我的主页”文件的 TabBar 小部件中的 isScrollable 属性。

对我来说,它是将 Expanded 小部件添加到 listView 的元素中,因此我将其删除并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失败的断言:第 551 行 pos 12:“child.hasSize”:不正确 - Failed assertion: line 551 pos 12: 'child.hasSize': is not true 断言失败:第 1979 行第 12 行:&#39;hasSize&#39; - Failed assertion: line 1979 pos 12: 'hasSize' 颤振错误:断言失败:第 1785 行 pos 12:&#39;hasSize&#39; - Flutter error:Failed assertion: line 1785 pos 12: 'hasSize' 断言行 5120 pos 12 失败:'child = _child' 不是真的 - Failed assertion line 5120 pos 12: 'child = _child' is not true 失败的断言:第 6075 行 pos 12:&#39;child == _child&#39;:不是真的 - Failed assertion: line 6075 pos 12: 'child == _child': is not true 在“Row”中渲染 ListTile 时出现错误“断言失败:第 1687 行 pos 12:'hasSize'” - Get an error `Failed assertion: line 1687 pos 12: 'hasSize'` when render ListTile inside `Row` 错误:RenderBox 未布局,断言失败:第 1940 行 pos 12:'hasSize' - Error: RenderBox was not laid out, Failed assertion: line 1940 pos 12: 'hasSize' 错误“child.hasSize”:不正确且受约束的框不起作用 - error `child.hasSize': is not true and constrained box not working 失败的断言:第 1696 行 pos 12:&#39;center!.parent == this&#39;:不是真的 - Failed assertion: line 1696 pos 12: 'center!.parent == this': is not true 断言失败:第 184 行 pos 12:'end &gt;= begin':不正确 - Failed assertion: line 184 pos 12: 'end >= begin': is not true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM