Reputation: 1676
I've met a error with dart:convert library.
Please help me resolve the problem. This is my block code:
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();
}
........
In the line of code, IDE shows error message 'Undfine name UTF8'
final body = await res.transform(UTF8.decoder).join();
Im using flutter version as below:
$ 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)
How can I fix this one ? Thanks!!!
Upvotes: 1
Views: 779
Reputation: 657761
The names were changed to lowerCamelCase in Dart 2. Seems you copied from an old example.
Use utf8
instead of UTF8
.
Upvotes: 4