[英]The mapping function from a local JSON file to a list is not working (flutter)
so, I'm trying to parse a list of questions into a list of objects of a class I created called Question所以,我正在尝试将问题列表解析为我创建的名为 Question 的 class 的对象列表
class Question {
final String question;
Question({this.question});
factory Question.fromJson(Map<String, dynamic> json) {
return new Question(question: json['question'] as String);
}
}
on calling the parse method, it gives me this error:在调用 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>***
the method is supposed to map the questions in JSON file to strings in Question objects该方法应该将 map 文件中的问题 JSON 文件中的问题对象中的字符串
here's the method这是方法
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();
}
}
after using cast instead, it gives this error改用 cast 后,它给出了这个错误
***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>***
here's the builder function of class QstnsList这是 class QstnsList 的构建器 function
@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 file content: 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?"
}
}
}
Maybe you can adjust your FutureBuilder like this也许你可以像这样调整你的 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());
},
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.