Flutter 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型

Flutter 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

我目前正在构建一个从 Api 获取数据的 Flutter 应用程序,但是当我 运行 该应用程序时,我收到错误 Flutter 'List' 不是类型 'Map<String, dynamic>' 的子类型。这是我的代码

    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":" WORLD","image":"01/A0000/06"},{"heading":"heading","story":"story","date":"date" ,"图片":"图片"}]

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

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

_map是一个Map变量,也就是说它可以用来存储单个地图元素。但是响应的 jsonDecode 的结果将是一个 Map 列表。所以把_map的声明改成

List> _map; 或者简单地 列表_map;