从本地JSON文件到列表的映射功能不起作用(颤振)

用户名

所以,我试图将问题列表解析为我创建的名为Question的类的对象列表

    class Question {
      final String question;

      Question({this.question});

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

    }

在调用parse方法时,它给了我这个错误:

***Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments. Receiver: _LinkedHashMap len:1 Tried calling: cast<Map<String, dynamic>>() Found: cast<RK, RV>() => Map<RK, RV>***

该方法应该将JSON文件中的问题映射到Question对象中的字符串

这是方法

List<Question> parseJson(String response) {
    if(response==null){
      return [];
    }
    final parsed =
    json.decode(response.toString()).cast<Map<String, dynamic>>();
    return parsed.map<Question>((json) => new Question.fromJson(json)).toList();
  }
}

在使用强制转换之后,它给出了这个错误

***Class 'CastMap<String, dynamic, String, dynamic>' has no instance method 'map' with matching arguments. Receiver: Instance of 'CastMap<String, dynamic, String, dynamic>' Tried calling: map<Question>(Closure: (dynamic) => Question) Found: map<K2, V2>((K, V) => MapEntry<K2, V2>) => Map<K2, V2>***

这是类QstnsList的builder函数

     @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: Text('list of questions'),),
          body: Center(
              child: FutureBuilder(
                future: DefaultAssetBundle.of(context).loadString("Files/AllQstnsj.JSON"),
                builder: (context, snapshot){
                List<Question> Questions = parseJson(snapshot.data.toString());
                return !Questions.isEmpty ?
                      new QuestionList(question: Questions,)
                    : new Center(child: new CircularProgressIndicator());
              },
              ),
          ),
        );
      }

json文件内容:

{
  "Questions": {
    "q1": {
      "question": "What embarrasses you the most and/or what’s been your most embarrassing moment?"
    },
    "q2": {
      "question": "Who do you look up to the most, and what qualities do you love about that person?"
    },
    "q3": {
      "question": "What would be your perfect day? Plan it out from waking up to bedtime."
    },
    "q4": {
      "question": "How do you like to be comforted when you’re sad or upset?"
    },
    "q5": {
      "question": "What’s your all-time favorite memory?"
    },
    "q6": {
      "question": "What’s your favorite thing you own and why?"
    },
    "q7": {
      "question": "Who was the last person who made you cry and why?"
    },
    "q8": {
      "question": "Where do you hope to be five years from now? Ten? Fifteen?"
    },
    "q9": {
      "question": "If money was no object, what would you buy?"
    },
    "q10": {
      "question": "When have you felt the most proud?"
    },
    "q11": {
      "question": "When have you felt the most challenged?"
    },
    "q12": {
      "question": "Do you have any regrets? If so, what are they?"
    },
    "q13": {
      "question": "When was the moment in your life you laughed the hardest?"
    },
    "q14": {
      "question": "Which celeb would you want as your other BFF and why?"
    },
    "q15": {
      "question": "What did you dream about last night?"
    },
    "q16": {
      "question": "Out of all your family members, who are you closest to?"
    },
    "q17": {
      "question": "What do you do when no one else is around?"
    },
    "q18": {
      "question": "Where do you want to travel the most?"
    },
    "q19": {
      "question": "What’s one thing most people don’t know about you?"
    },
    "q20": {
      "question": "If you could only eat one thing for the rest of your life, what would it be?"
    },
    "q21": {
      "question": "What book has impacted you the most?"
    },
    "q22": {
      "question": "If you had three wishes, what would you wish for?"
    },
    "q23": {
      "question": "Do you want to have kids one day? How many?"
    },
    "q24": {
      "question": "What's the best present you ever received? Who gave it to you and why was it so special?"
    },
    "q25": {
      "question": "If you had to choose only one, would pick a dog or a cat?"
    },
    "q26": {
      "question": "Do you believe in aliens?"
    },
    "q27": {
      "question": "If you were President, what's the first thing you would work to change in this country?;"
    },
    "q28": {
      "question": "What college major are you thinking of choosing and why?"
    },
    "q29": {
      "question": "f you could only listen to one singer or band for the rest of your life, who would you choose?"
    },
    "q30": {
      "question": "You just won $10,000 — what are you going to do with the money?"
    },
    "q31": {
      "question": "What's your go-to dish when you have to make dinner?"
    },
    "q32": {
      "question": "What's the worst you've ever been physically hurt?"
    },
    "q33": {
      "question": "What's your earliest childhood memory?"
    },
    "q34": {
      "question": "If you had to live in another time period, what would you choose?"
    },
    "q35": {
      "question": "Who was your very first crush?"
    },
    "q36": {
      "question": "What foreign language(s) would you want to master and why?"
    },
    "q37": {
      "question": "What's your all-time favorite dessert?"
    },
    "q38": {
      "question": "If you could change one thing about yourself, what would it be?"
    },
    "q39": {
      "question": "What's the luckiest thing that's ever happened to you?"
    },
    "q40": {
      "question": "What's your favorite way to exercise?"
    },
    "q41": {
      "question": "Do you like roller coasters?"
    },
    "q42": {
      "question": "If you had to describe yourself in only three words, what would you choose? What words do you think your friends would choose to describe you?"
    },
    "q43": {
      "question": "Do you believe in soulmates?"
    },
    "q44": {
      "question": "What is your most hated chore and why?"
    },
    "q45": {
      "question": "What's your favorite holiday and why?"
    },
    "q46": {
      "question": "When you can't sleep, what keeps you up at night?"
    },
    "q47": {
      "question": "If you were an animal, what would you be?"
    },
    "q48": {
      "question": "If you had to create an alter ego for yourself, who would it be and what would you name them?"
    },
    "q49": {
      "question": "Do you have any phobias?"
    },
    "q50": {
      "question": "What are your top three biggest pet peeves?"
    },
    "q51": {
      "question": "What has your hardest goodbye in life been so far?"
    },
    "q52": {
      "question": "What would your dream vacay consist of?"
    },
    "q53": {
      "question": "What's your absolute dream job?"
    },
    "q54": {
      "question": "If you could have a superpower, what would you choose?"
    },
    "q55": {
      "question": "What TV show do you turn on when you just want to zone out and feel better?"
    },
    "q56": {
      "question": "What was the last movie you watched in a theater?"
    },
    "q57": {
      "question": "What's the last lie you told?"
    },
    "q58": {
      "question": "What's the best compliment you've ever received?"
    },
    "q59": {
      "question": "What do you consider the ultimate comfort food?"
    },
    "q60": {
      "question": "Do you believe in ghosts?"
    },
    "q61": {
      "question": "What's your favorite color and what's your least favorite color?"
    },
    "q62": {
      "question": "What do you consider your best physical feature?"
    },
    "q63": {
      "question": "What are the top three things that stress you out?"
    },
    "q64": {
      "question": "What are the top three ways you deal with stress?"
    },
    "q65": {
      "question": "When was the last time you were jealous and why?"
    },
    "q66": {
      "question": "If you could time travel, where would you go?"
    },
    "q67": {
      "question": "What do you usually have for breakfast?"
    },
    "q68": {
      "question": "What's your favorite plant or flower?"
    }
  }
}
dm_tr

也许您可以像这样调整FutureBuilder

FutureBuilder(
  future: DefaultAssetBundle.of(context).loadString("Files/AllQstnsj.JSON"),
  builder: (context, snapshot) {
    List<Question> questions = [];
    if (snapshot.hasData) {
      var data = jsonDecode(snapshot.data);
      data["Questions"].forEach((key, value) {
         questions.add(Question.fromJson(value));
      });
    }
    return questions.isNotEmpty ? new QuestionList(question: questions,) : new Center(child: new CircularProgressIndicator());
  },
),

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

颤振中的映射功能在列窗口小部件中不起作用

来自分类Dev

功能不起作用

来自分类Dev

功能不起作用

来自分类Dev

列表产品的递归功能不起作用

来自分类Dev

对象列表功能不起作用

来自分类Dev

Cygwin终端GDB列表功能不起作用

来自分类Dev

读取文件的功能不起作用。互动菜单问题

来自分类Dev

Python-分隔文件中的功能不起作用

来自分类Dev

显示文件的功能不起作用-Python

来自分类Dev

MediaPlayer的字幕功能不起作用(SRT文件)

来自分类Dev

velocity.js文件存在,但功能不起作用

来自分类Dev

Octopus 部署 JSON 配置变量功能不起作用

来自分类Dev

颤振生成命令不起作用收到错误

来自分类Dev

颤振平面按钮颜色属性不起作用

来自分类Dev

容器BorderRadius.circular不起作用(颤振)

来自分类Dev

回调函数在颤振中不起作用

来自分类Dev

“颤振医生”卡住了,颤振命令都不起作用

来自分类Dev

异步功能不起作用

来自分类Dev

CSS:悬停功能不起作用

来自分类Dev

scons命令功能不起作用

来自分类Dev

JavaScript onClick功能不起作用

来自分类Dev

JavaScript的替换功能不起作用

来自分类Dev

删除功能不起作用

来自分类Dev

Ng重复功能不起作用

来自分类Dev

简单的计算功能不起作用

来自分类Dev

$ .ajax错误功能不起作用

来自分类Dev

Protobuf“ oneof”功能不起作用

来自分类Dev

爆炸功能不起作用

来自分类Dev

bash中的功能不起作用