Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 颤振小部件状态构造函数未运行_Dart_Flutter - Fatal编程技术网

Dart 颤振小部件状态构造函数未运行

Dart 颤振小部件状态构造函数未运行,dart,flutter,Dart,Flutter,我的代码: import 'package:flutter/material.dart'; import '../services.dart'; import 'PersonCard.dart'; class PersonList extends StatefulWidget { PersonList() { Services.fetchPeople(); } @override _PersonListState createState() => new _

我的代码:

import 'package:flutter/material.dart';
import '../services.dart';
import 'PersonCard.dart';

class PersonList extends StatefulWidget {  
  PersonList() {
    Services.fetchPeople();
  }

  @override
  _PersonListState createState() => new _PersonListState();
}

class _PersonListState extends State<PersonList> {    
  _PersonListState() {
    print('Helo!?'); // Not running
  }

  @override
  Widget build(BuildContext context) {
    return new Padding(
      padding: new EdgeInsets.all(10.0),
      child: new ListView(
        children: <Widget>[
          // new PersonCard('Bob', 'Wazowski', '2 minutes ago'),
          // new PersonCard('Tim', 'Alan', '5 hours ago'),
          // new PersonCard('Josh', 'Applebee', 'Yesterday'),
        ]
      )
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“../services.dart”;
导入“PersonCard.dart”;
类PersonList扩展StatefulWidget{
个人主义者(){
Services.fetchPeople();
}
@凌驾
_PersonListState createState()=>new_PersonListState();
}
类_PersonListState扩展了状态{
_PersonListState(){
打印('Helo!?');//未运行
}
@凌驾
小部件构建(构建上下文){
返回新的填充(
填充:新边缘设置。全部(10.0),
子:新列表视图(
儿童:[
//new PersonCard(“鲍勃”、“瓦佐夫斯基”、“2分钟前”),
//新人卡(“蒂姆”、“艾伦”、“5小时前”),
//新人卡(“Josh”、“Applebee”、“昨天”),
]
)
);
}
}
基本上,当我的小部件加载
PersonList()
函数运行时,该函数使用
Services.fetchPeople()
调用web服务。基本上,我想做的是
fetchPeople()
将返回一个
Map
,然后我可以迭代它并创建
PersonCard
对象


但是
\u PersonListState()
构造函数没有运行。我是否遗漏了一些明显的内容?

您可以使用
widget.people.map((p)=>new PersonCard(…)
将数据转换为Wiget:

class PersonList extends StatefulWidget {  
  PersonList() {
    Services.fetchPeople().then((p) => setState(() => people = p));
  }

  List<Person> people;

  @override
  _PersonListState createState() => new _PersonListState();
}

class _PersonListState extends State<PersonList> {    
  _PersonListState() {
    print('Helo!?'); // Not running
  }

  @override
  Widget build(BuildContext context) {
    return new Padding(
      padding: new EdgeInsets.all(10.0),
      child: new ListView(
        children: widget.people.map(
            (p) => new PersonCard(p.firstName, p.lastName, p.updateTime /* or whatever that is */,
        ).toList(),
      ),
    );
  }
}
class PersonList扩展了StatefulWidget{
个人主义者(){
然后((p)=>setState(()=>people=p));
}
列出人员名单;
@凌驾
_PersonListState createState()=>new_PersonListState();
}
类_PersonListState扩展了状态{
_PersonListState(){
打印('Helo!?');//未运行
}
@凌驾
小部件构建(构建上下文){
返回新的填充(
填充:新边缘设置。全部(10.0),
子:新列表视图(
儿童:widget.people.map(
(p) =>新的PersonCard(p.firstName、p.lastName、p.updateTime/*或其他类似名称*/,,
).toList(),
),
);
}
}

在何处或如何使用
PersonList
?当
Services.fetchPeople()
返回时,它是否不返回值?您需要在某个位置分配此值。我收到错误:“参数类型'Iterable'无法分配给参数类型'List”当我试图映射widget.people并传递给“children”propI时,我认为我错过了一个
(如果没有合适的IDE,有点难以看到)
Iterable
听起来像是您忽略了
toList()
。但是不确定
。你能更新你的问题并添加你正在使用的具体代码吗?