我在dart:convert库中遇到错误。
请帮助我解决问题。这是我的阻止代码:
import 'dart:async' show Future, Stream;
import 'dart:convert' show json, UTF8;
import 'dart:io' show HttpClient;
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart' show rootBundle;
/// PODO for questions
class Question {
String question;
bool answer;
Question.fromJson(Map jsonMap) :
question = jsonMap['question'],
answer = jsonMap['answer'];
String toString() {
return '$question is $answer';
}
}
Future<List<Question>> loadQuestionsLocally() async {
final jsonString = await rootBundle.loadString('assets/questions.json');
final questions = json.decode(jsonString);
return questions.map(
(q) => new Question.fromJson(q)
).toList();
}
Future<List<Question>> loadQuestionsNetwork() async {
final req = await new HttpClient().getUrl(
Uri.parse('https://raw.githubusercontent.com/mjohnsullivan/flutter_quiz/master/assets/questions.json')
);
final res = await req.close();
final body = await res.transform(UTF8.decoder).join();
final questions = json.decode(body);
return questions.map(
(q) => new Question.fromJson(q)
).toList();
}
........
在代码行中,IDE显示错误消息'Undfine name UTF8'
最终主体=等待res.transform(UTF8.decoder).join();
我使用的是flutter版本,如下所示:
$ flutter --version
Flutter 1.2.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8661d8aecd (3 weeks ago) • 2019-02-14 19:19:53 -0800
Engine • revision 3757390fa4
Tools • Dart 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)
该如何解决? 谢谢!!!
答案 0 :(得分:3)
在Dart 2中,名称已更改为lowerCamelCase。似乎是从旧示例复制过来的。
使用utf8
代替UTF8
。