gpt4 book ai didi

flutter - flutter一个构建函数返回null

转载 作者:行者123 更新时间:2023-12-03 04:39:22 25 4
gpt4 key购买 nike

我正在尝试使用FutureBuilder,但其显示的A构建函数错误返回null
我的密码

class _EventsState extends State<Events> {
@override

Future<List> doSomeAsyncStuff() async {
final storage = new FlutterSecureStorage();
String value = await storage.read(key: 'token');
print(value);

String url = 'http://sublimeapi.netcodesolution.com/api/NewsAndEvents/';

String token = value;
final response = await http.get(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
});
print('Token : ${token}');
var eventData = json.decode(response.body);
print(eventData["Data"]);
List _events = eventData["Data"];
return _events;
}

@override
Widget build(BuildContext context) {
double statusBarHeight = MediaQuery
.of(context)
.padding
.top;
return Expanded(
child: FutureBuilder(
future: doSomeAsyncStuff(),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
List<Widget> children;
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print('working');
print(snapshot.data);
return Container(
child: Column(
children: <Widget>[
Text('working')
],
),
);
}
}
}),
);
}
正如您在代码中看到的那样,我正在从API提取数据并且其工作正常。在代码中,我在setState中打印_events的值,它也这样打印值
I/flutter (32627): [{Id: 38, Description: Two days Workshop on Prevention of Suicide organized for GPs of Thar., ImagePath: /journals/2e8a55f3-6612-4b23-a0ea-e91022c159a8.pdf, CreatedBy: 4447, CreatedOn: 2019-09-18T14:56:13.357, Active: false, Type: Events, Attachment: null, AttachmentType: Events}
我需要在以后的小部件中打印此数据的Description值,但不知道为什么它显示错误

最佳答案

错误清楚地说明了!它返回空值。
因此,您必须退货!做这样的事情,

 Future<List> doSomeAsyncStuff() async {
final storage = new FlutterSecureStorage();
String value = await storage.read(key: 'token');
print(value);

String url = 'http://sublimeapi.netcodesolution.com/api/NewsAndEvents/';

String token = value;
final response = await http.get(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
});
print('Token : ${token}');
var eventData = json.decode(response.body);
print(eventData["Data"]);
List _events = eventData["Data"];
return _events;
}
而且,我们在这里错过了另一个案例。
 Scaffold(
appbar: AppBar(
title: const Text('Sample Future Builder'),
),
body: Expanded(
child: FutureBuilder(
future: doSomeAsyncStuff(),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {

if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print('working');
print(snapshot.data);
return Container(
child: Column(
children: <Widget>[
Text('working')
],
),
);
}
}
return Center(child: Text("Not Loaded Yet!!"),)
}
),
),
);
希望能解决您的问题!

Tip: Move all your widgets under Scaffold. It would be the best practice. Refer this

关于flutter - flutter一个构建函数返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63526559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com