【问题标题】:A value of type 'Future<List<Question>>' can't be assigned to a variable of type 'List<Question>'“Future<List<Question>>”类型的值不能分配给“List<Question>”类型的变量
【发布时间】:2020-06-01 09:37:34
【问题描述】:
import 'dart:async';
import 'question.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

String opentdb = 'https://opentdb.com/api.php?amount=15&type=boolean';

class QuestionServices {
  Future<List<Question>> getData() async {
    List<Question> questions;
    String link = opentdb;
    var res = await http
        .get(Uri.encodeFull(link), headers: {"Accept": "application/json"});
    print(res.body);
    if (res.statusCode == 200) {
      var data = json.decode(res.body);
      var rest = data['results'] as List;
      print(rest);
      questions =
          rest.map<Question>((json) => Question.fromJson(json)).toList();
    }
    print("List Size: ${questions.length}");
    // _questions = questions;
    return questions;
  }

  List<Question> newQuestions = getData();
}
class Question {
  final String question;
  final bool answer;

  Question({this.question, this.answer});

  factory Question.fromJson(Map<String, dynamic> json) {
    return Question(
      question: json['question'] as String,
      answer: json['correct_answer'] as bool,
    );
  }
}

我正在尝试从 JSON 数据库创建问题列表,但每当我尝试获取返回的列表时,我都会收到错误消息:

"A value of type 'Future<List<Question>>' can't be assigned to a variable of type 'List<Question>'."

我不确定为什么我返回的 List 会发出该错误。是否有其他方法可以将 json 加入列表?

【问题讨论】:

  • 列表 newQuestions = await getData();您可能错过了“等待”关键字

标签: json flutter dart


【解决方案1】:
List<Question> getData() async { // remove Future
    List<Question> questions;
    String link = opentdb;
 ...


//how to access now you will get instance of List<Question>

getData().then((List<Question> newQuestions){

})

【讨论】:

    【解决方案2】:

    getData 返回一个Future,因此您需要执行以下操作:

    Future<List<Question>> newQuestions = getData();
    

    【讨论】:

      【解决方案3】:
      Future<List<Question>>Listfetchdata() async{
         final responsein = await http.get('https://opentdb.com/api.php?amount=15&type=boolean');
         final getindata = json.decode(responsein.body);
         final getindata1 = json.encode(getindata['results']);
         return compute(listdata,getindata1);
       }
      
       List<Question> listdata(String creation){
         final parsed = jsonDecode(creation).cast<Map<String,dynamic>>();
         return parsed.map<Question>((json)=>Question.fromJson(json)).toList();
       }
      
      List listitem;
       var maketolistarry=[''];
       void lastgettingdata() async{
         final king=await Listfetchdata();
      listitem=king.map((e) => e.category).toList();
      maketolistarry=listitem;
       }
      

      在 maketolistarry 的帮助下你会得到一个列表

      【讨论】:

        猜你喜欢
        • 2021-08-29
        • 2021-11-07
        • 2020-10-24
        • 1970-01-01
        • 2021-10-08
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多