gpt4 book ai didi

json - Flutter 'List' 不是类型 'Map' 的子类型

转载 作者:行者123 更新时间:2023-12-01 23:04:34 28 4
gpt4 key购买 nike

我目前正在构建一个从 Api 获取数据的 Flutter 应用程序,但是当我运行该应用程序时出现错误 Flutter 'List' is not a subtype of type 'Map ' .这是我的代码

    class NewsData with ChangeNotifier 
{

Map<String,dynamic> _map = {};
bool _error = false;
String _errorMessage = '';

Map<String,dynamic> get map => _map;
bool get error => _error;
String get errorMessage => _errorMessage;

Future<void> get fetchdata async {
final response = await get(Uri.parse('https://script.google.com/macros/s/AKfycbz3ZmaMiTgR-y70MEE1v9VPz2QtmNzwnnnnnnn/exec'),);
if (response.statusCode == 200)
{
try
{
_map = jsonDecode(response.body);
_error = false;
}
catch(e) {
_error = true;
_errorMessage = e.toString();
_map = {};
}
}
else
{
_error = true;
_errorMessage = 'Error :it could be your internet connection ';
_map = {};
}
notifyListeners();
}
void initialValues()
{
_map = {};
_error = false;
_errorMessage = '';
notifyListeners();
}
}


stories.dart

class Storiespage extends StatelessWidget {
const Storiespage({ Key? key }) : super(key: key);

@override
Widget build(BuildContext context) {
context.read<NewsData>().fetchdata;
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.refresh),
onPressed:() {
context.read<NewsData>().initialValues();
context.read<NewsData>().fetchdata;
},
)
],
title: Text('ggg'),
),
body: RefreshIndicator(
onRefresh: () async {
await context.read<NewsData>().fetchdata;
},
child: Center(
child: Consumer<NewsData>(
builder: (context, value, child) {
return value.map.length == 0 && !value.error
? CircularProgressIndicator()
: value.error ? Text('oops,something went wrong ${value.errorMessage}'
,textAlign: TextAlign.center,) :
ListView.builder(
itemCount: value.map['feedback'].length,
itemBuilder: (context,index) {


return Newscard(map: value.map['feedback'][index]);
},
);
},
),
),
),
);
}
}

class Newscard extends StatelessWidget {
const Newscard({ Key? key,required this.map }) : super(key: key);
final Map<String,dynamic> map;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
elevation:10 ,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.network('${map['image']}'),
SizedBox(
height: 10,
),
Text('${map['heading']}'
),
SizedBox(
height: 10,
),
Text('${map['story']}'
),
],

),
),
),
);
}
}

和json文件[{"heading":"admire","story":2021,"date":"WORLD","image":"01/A0000/06"},{"heading":"admire","story":2021 ,"date":"世界","image":"01/A0000/06"},{"heading":"heading","story":"story","date":"date","image":"图片"}]

最佳答案

发生错误是因为您的 API 返回 List正如您在 json 文件中看到的那样。使用 jsonDecode 后你的解码类型是List<dynamic> .要解决您的问题,您必须更改 type_mapList<dynamic> .如果你想要类型 List<Map<String, dynamic>>您可以执行以下操作。

List<Map<String, dynamic>> map = [];
...
_map = List<Map<String, dynamic>>.from(jsonDecode(response.body));

关于json - Flutter 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71192927/

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