Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何获得省道颤振图?_Flutter_Dart - Fatal编程技术网

Flutter 如何获得省道颤振图?

Flutter 如何获得省道颤振图?,flutter,dart,Flutter,Dart,我有这种错误, 类型列表不是类型映射的子类型,我已经搜索了很多问题,仍然无法理解该错误的目的。这是我的api代码 import 'dart:convert'; import 'package:learn_how_to_fetch/announcement.dart'; import 'package:requests/requests.dart'; Future<Announcement> fetchAnnouncement() async { Map<String, St

我有这种错误,
类型列表不是类型映射的子类型
,我已经搜索了很多问题,仍然无法理解该错误的目的。这是我的api代码

import 'dart:convert';

import 'package:learn_how_to_fetch/announcement.dart';
import 'package:requests/requests.dart';

Future<Announcement> fetchAnnouncement() async {
Map<String, String> headers = {
  'Content-Type': 'application/json;charset=UTF-8',
  'Charset': 'utf-8'
};
var response = await Requests.get('http://172.16.54.2:5000/api/announcelist',
  verify: false, headers: headers);
 response.raiseForStatus();
 // final jsonTried = json.decode(response.content()) as List;
 // print(json);
 if (response.statusCode == 200) {
 final jsonResponse = json.decode(response.content());
 // print(jsonResponse
 List<Announcement> anonList = []
 Announcement anonResponse = Announcement.fromJson(jsonResponse);
 print(anonResponse.id);
 return anonResponse;
 } else {
 // If the server did not return a 200 OK response,
 // then throw an exception.
  throw Exception('Failed to load album');
  } 
 }  
导入'dart:convert';
导入“package:learn_how_fetch/announcement.dart”;
导入“package:requests/requests.dart”;

将来,但我仍然无法解决这个问题

问题是,您从服务器获得一个JSON列表

在json列表上使用json.decode()返回一个列表


但是您的代码期望jsonResponse是一个映射。

问题是,您从服务器获得一个JSON列表

在json列表上使用json.decode()返回一个列表


但是您的代码期望jsonResponse是一个映射。

您应该首先在列表中获取服务器响应,然后将所有元素转换为自定义类,即Announcement。请尝试以下代码:

Future<Announcement> fetchAnnouncement() async {
  Map<String, String> headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    'Charset': 'utf-8'
  };
  var response = await Requests.get('http://172.16.54.2:5000/api/announcelist',
      verify: false, headers: headers);
  response.raiseForStatus();
  // final jsonTried = json.decode(response.content()) as List;
  // print(json);
  if (response.statusCode == 200) {
    final jsonResponse = json.decode(response.content());
    // print(jsonResponse
    List<dynamic> anonList = jsonResponse;
    List<Announcement> anonResponse = [];
    for (int i = 0; i < anonList.length; i++) {
      anonResponse.add(Announcement.fromJson(anonList[i]));
    }
    print(anonResponse);
    return anonResponse[0];
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}
Future fetchAnnouncement()异步{
映射头={
“内容类型”:“应用程序/json;字符集=UTF-8”,
“字符集”:“utf-8”
};
var response=等待请求。获取('http://172.16.54.2:5000/api/announcelist',
验证:假,标题:标题);
response.raiseForStatus();
//final jsonTried=json.decode(response.content())作为列表;
//打印(json);
如果(response.statusCode==200){
final jsonResponse=json.decode(response.content());
//打印(jsonResponse)
List anonList=jsonResponse;
列表anonResponse=[];
for(int i=0;i
您应该首先在列表中获取服务器响应,然后将所有元素转换为自定义类,即公告。请尝试以下代码:

Future<Announcement> fetchAnnouncement() async {
  Map<String, String> headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    'Charset': 'utf-8'
  };
  var response = await Requests.get('http://172.16.54.2:5000/api/announcelist',
      verify: false, headers: headers);
  response.raiseForStatus();
  // final jsonTried = json.decode(response.content()) as List;
  // print(json);
  if (response.statusCode == 200) {
    final jsonResponse = json.decode(response.content());
    // print(jsonResponse
    List<dynamic> anonList = jsonResponse;
    List<Announcement> anonResponse = [];
    for (int i = 0; i < anonList.length; i++) {
      anonResponse.add(Announcement.fromJson(anonList[i]));
    }
    print(anonResponse);
    return anonResponse[0];
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}
Future fetchAnnouncement()异步{
映射头={
“内容类型”:“应用程序/json;字符集=UTF-8”,
“字符集”:“utf-8”
};
var response=等待请求。获取('http://172.16.54.2:5000/api/announcelist',
验证:假,标题:标题);
response.raiseForStatus();
//final jsonTried=json.decode(response.content())作为列表;
//打印(json);
如果(response.statusCode==200){
final jsonResponse=json.decode(response.content());
//打印(jsonResponse)
List anonList=jsonResponse;
列表anonResponse=[];
for(int i=0;i
请在您的问题中添加服务器响应(您期望的JSON)。您的意思是什么?您指的是这一个吗?
final jsonResponse=JSON.decode(response.content());
否,我指的是您从服务器获得的数据(您从该服务器上发布了公告类)。但是我得到的数据是在您提供的服务器响应上的,它是否包含方括号?请在您的问题中添加服务器响应(您期望的JSON)。您的意思是什么?您是指这一个吗?
final jsonResponse=JSON.decode(response.content());
no,我是指您从服务器获得的数据(您在其中发布了公告课程)。但是我得到的数据在您提供的服务器响应上,它是否包含方括号?那么我应该怎么做?这取决于您试图实现的目标。该响应中显然有多个公告。因此,例如,您可以返回如下公告列表:List Announcements=jsonResponse.map((json)=>Announcement.fromJson(json)).toList();@RickyRezaMuhammad您需要将此行从Announcement.fromJson(映射json)更改为Announcement.fromJson(列表json)如果我改变,我将得到一个错误参数字符串不能被分配到类型Int,而我改变toList,我将得到这个错误类型列表不是类型FutureOrso的子类型,我该怎么办?这取决于你试图实现什么。显然,在该响应中有多个声明。例如,你可以返回一个类似这样的公告:List Announcements=jsonResponse.map((json)=>Announcement.fromJson(json)).toList();@RickyRezaMuhammad您需要将此行从Announcement.fromJson(map json)更改为Announcement.fromJson(List json)如果我改变,我将得到一个错误参数字符串不能被分配到类型Int,我将得到这个错误类型列表不是类型FutureOrwow的子类型,这很好,但它只返回1个数据,那么我如何返回所有数据?我应该改变公告吗?或者?返回整个列表:你应该更改将函数类型从未来返回到未来,并返回整个列表,即返回anonResponse;此外,您的构建函数也会相应地进行一些更改。哇,这很好,但它当时只返回1个数据,那么我如何才能返回所有数据?我应该更改公告吗?还是?要返回整个列表:您应该更改返回从Future到Future的函数类型,并返回整个列表,即returnanonresponse;此外,构建函数也会相应地进行一些更改。
  [{
    "id": 57,
    "title": "Title",
    "image": "image/specialitem/SPI1454050237.jpg",
    "content": "Something",
    "text": "<Content>",
    "active": 0
 },
{...}]
Future<Announcement> fetchAnnouncement() async {
  Map<String, String> headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    'Charset': 'utf-8'
  };
  var response = await Requests.get('http://172.16.54.2:5000/api/announcelist',
      verify: false, headers: headers);
  response.raiseForStatus();
  // final jsonTried = json.decode(response.content()) as List;
  // print(json);
  if (response.statusCode == 200) {
    final jsonResponse = json.decode(response.content());
    // print(jsonResponse
    List<dynamic> anonList = jsonResponse;
    List<Announcement> anonResponse = [];
    for (int i = 0; i < anonList.length; i++) {
      anonResponse.add(Announcement.fromJson(anonList[i]));
    }
    print(anonResponse);
    return anonResponse[0];
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}