_TypeError 在构建 FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic> type 'N
Posted
技术标签:
【中文标题】_TypeError 在构建 FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic> type \'Null\' is not a subtype of type \'List<dynamic>\'【英文标题】:_TypeError was thrown building FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic> type 'Null' is not a subtype of type 'List<dynamic>'_TypeError 在构建 FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic> type 'Null' is not a subtype of type 'List<dynamic>' 【发布时间】:2021-09-08 17:21:26 【问题描述】:# 我已经处理这个问题很长时间了。 我正在尝试解析 JSON 并将其转换为列表视图。 我得到了响应正文,它也被转换为列表,但它向未来的构建器发送 null,我收到此错误: #
小部件库捕获的异常 =========================================== ============== 在构建 FutureBuilder(dirty, state: _FutureBuilderState#a289a) 时引发了以下 _TypeError: “Null”类型不是“List”类型的子类型
The relevant error-causing widget was:
FutureBuilder<dynamic> file:///Users/metehanmacbook/StudioProjects/flutter_mysql/lib/main.dart:37:13
When the exception was thrown, this was the stack:
#0 _MyHomePageState.build.<anonymous closure> (package:flutter_mysql/main.dart:40:14)
#1 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:775:55)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
...
**# Codes: #**
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main()
runApp(MyApp());
class MyApp extends StatelessWidget
// This widget is the root of your application.
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter MySql',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.lime,
),
home: MyHomePage(title: 'Flutter MYSQL'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key? key, required this.title) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: Getmethod(),
builder: (BuildContext context, AsyncSnapshot snapshot)
List snap = snapshot.data;
if(snapshot.connectionState == ConnectionState.waiting)
return Center(
child: CircularProgressIndicator(),
);
if(snapshot.hasError)
return Center(
child: Text('error fatch'),
);
return ListView.builder(
itemCount: snap.length,
itemBuilder: (context, index)
return ListTile(
title: Text("Product Name: $snap[index]['pro_name']"),
subtitle: Text("Product Desc: $snap[index]['pro_desc']"),
);
);
,
),
);
Getmethod()async
String theurl = ('http://10.0.2.2/flutter-demo/getdata.php');
var res = await http.get(Uri.parse(Uri.encodeFull(theurl)),headers: "Accept":"application/json");
var responseBody = json.decode(res.body);
print(responseBody);
return responseBody;
我该如何解决这个问题?
【问题讨论】:
【参考方案1】:似乎错误是List snap = snapshot.data;
snapshot.data 为空,您想将其转换为列表。
解决方案:
-
您可以将
snap
变量设为nullable
: List? snap = snapshot.data;
如果snap
返回null
,您可以返回默认值(例如空列表):List snap = snapshot.data ?? [];
调查您的Getmethod
为何返回null
;
【讨论】:
以上是关于_TypeError 在构建 FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic> type 'N的主要内容,如果未能解决你的问题,请参考以下文章
TypeError: rxjs_1.lastValueFrom 不是函数
NestJS 和 GraphQL - TypeError:graphql_1.parse 不是具有最少设置的函数
TypeError.无法读取未定义的'$__'属性。无法读取未定义的'$__'属性 - Mongoose v5.^。
angular_1.default.module Uncaught TypeError:无法读取未定义的属性“模块”
Subparsers.add_parser TypeError: __init__() 得到了一个意外的关键字参数“prog”