【问题标题】:Expected a value of type 'Map<List<dynamic>, dynamic>' but got one of type 'List<dynamic>' while making api post request需要一个“Map<List<dynamic>, dynamic>”类型的值,但在发出 api post 请求时得到一个“List<dynamic>”类型的值
【发布时间】:2021-11-12 05:12:36
【问题描述】:

我正在构建一个颤振网页,其中包含从 api 检索的数据,几乎一切正常,但我遇到了这个问题。该api只接受一个参数'{“url”:“”}',并且我的代码中的所有内容都有效,因为如果我尝试在控制台中打印结果,它就可以工作。我唯一的问题是在屏幕上显示数据时,即使我将他期望的类型传递给他,颤振也给了我这个错误。

这是我的代码:

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

Future<Album> fetchAlbum() async {
  final headers = {"Content-type": "text/plain"};
  final json = '{"url": ""}';
  final response = await post(
      Uri.parse('myapikey'),
      headers: headers,
      body: json);

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    print(response.body);
    return Album.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

class Album {
  final String url;

  Album({
    required this.url,
  });

  factory Album.fromJson(Map<List<dynamic>, dynamic> json) {
    return Album(url: json['']);
  }
}

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late Future<Album> futureAlbum;

  @override
  void initState() {
    super.initState();
    futureAlbum = fetchAlbum();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Test'),
        ),
        body: Center(
          child: FutureBuilder<Album>(
            future: futureAlbum,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data!.url);
              } else if (snapshot.hasError) {
                return Text('${snapshot.error}');
              }

              // By default, show a loading spinner.
              return const CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
}

有人可以帮我吗?

【问题讨论】:

  • 参考我的回答here希望对你有所帮助
  • 您好,感谢您的回答。我之前尝试过这个,但得到了同样的错误,因为我的 api 接受的参数只有一个,而不是像 json 占位符那样的 3 个
  • 你能添加你的 API url
  • 不,很遗憾,我不能,这不是我的财产,所以出于安全原因,我不能发布网址

标签: flutter api dart flutter-web


【解决方案1】:

Album 类中的工厂需要 Map 具有 List 类型的键和 动态类型的值,但您的 json 正文是 {"url": ""},解码后它带有jsonDecode(response.body),类型为Map&lt;String, String&gt;,而这IS NOT THE SAME与Map&lt;List&lt;dynamic&gt;, dynamic&gt;。

class Album {
  final String url;

  Album({
    required this.url,
  });
  
  // This factory causes the error
  factory Album.fromJson(Map<List<dynamic>, dynamic> json) {
    return Album(url: json['']);
  }
}

此外,当您要检索 url 的值时,您必须编写 json["url"] 而不是 json[''],因为 "url" 是 "" 在 '{"url": ""}' 中的键。

解决方案:因此您应该更改工厂以接受Map&lt;dynamic, dynamic&gt; 类型的参数,并将用于检索"" 的密钥从json[''] 更改为json["url"]。

  factory Album.fromJson(Map<dynamic, dynamic> json) {
    return Album(url: json["url"]);
  }

另请参阅此工作 DartPad 示例:

import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart';

Future<Album> fetchAlbum() async {
  final headers = {"Content-type": "text/plain"};
  final json = '{"url": ""}';
  final response = Response(
    json,
    200,
    request: Request(
      "post",
      Uri.parse('myapikey'),
    ),
    headers: headers,
  );

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    print(response.body);
    return Album.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

class Album {
  final String url;

  Album({
    required this.url,
  });

  factory Album.fromJson(Map<dynamic, dynamic> json) {
    return Album(url: json["url"]);
  }
}

void main() {
  fetchAlbum();
}

【讨论】:

  • 您好,感谢您的回答。我尝试了您的解决方案,问题从“预期类型为 'Map, dynamic>' 但在发出 api 发布请求时得到类型为 'List'”到“预期值为输入“地图”,但得到了“列表”类型之一。”所以它或多或少是相同的错误。我还尝试了您建议我的解决方案(通过 DartPad 工作的解决方案),但是使用此代码,控制台 response.body 变成了 {"url":""} 而不是 api 中的数据,所以这就是它在控制台中打印的内容
  • 在 dartpad 示例中,我自己构造了一个 Response 对象(final response = Response(...)),而不是使用 post 方法,所以显然 body 不会改变。尝试在您的问题 (final response = await post(...)) 中使用带有 API 密钥的 post 方法。
猜你喜欢
  • 2021-06-12
  • 2021-07-13
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2020-05-19
  • 2021-05-02
  • 2021-07-29
相关资源
最近更新 更多