Select Git revision
game_utils.dart
random_pick_category.dart 868 B
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'dart:math' show Random;
class RandomPickCategory {
RandomPickCategory();
String _category;
final random = Random();
init() async {
await categoryFromLocalFile();
}
Future<void> categoryFromLocalFile() async {
String jsonString;
try {
jsonString = await rootBundle.loadString('assets/files/categories-fr.json');
final jsonResponse = await json.decode(jsonString);
var categoryList = jsonResponse[jsonResponse.keys.toList().join()];
int randomCategoryIndex = random.nextInt(categoryList.length);
String category = categoryList[random.nextInt(categoryList.length)];
_category = category ?? 'UNEXPECTED ERROR';
} catch (e) {
_category = 'UNEXPECTED ERROR';
}
}
String get category => _category;
}