Open orl0pl opened 1 month ago
I fixed this issue in my app by copying the content from quotes.json
to multiline string constant in new file called quotesystring.dart
and copying quotesy.dart
modifying one line of code.
quotesy.dart
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import './quotesystring.dart';
/// Class holding a quote information
class QuoteInfo {
final String author;
final String text;
final String? source;
final String? tags;
QuoteInfo(this.text, this.author, {this.source, this.tags});
factory QuoteInfo.fromJson(Map<String, dynamic> json) {
return QuoteInfo(
json['text'],
json['author'],
source: json['source'],
tags: json['tags'],
);
}
}
/// Static class that can be invoked to fetch quotes
class Quotes {
/// Loads the information from the `.json` file containing the quotes list.
static Future<List<QuoteInfo>> _quotesList() async {
+ String jsonString = quotesyStringJson;
List<dynamic> jsonList = json.decode(jsonString);
List<QuoteInfo> quotes =
jsonList.map((json) => QuoteInfo.fromJson(json)).toList();
return quotes;
}
/// Returns a list of quotes
static list() async {
return await _quotesList();
}
/// Returns a random quote
static Future<QuoteInfo> random() async {
final random = Random();
final list = await _quotesList();
return list[random.nextInt(list.length)];
}
/// Returns a list of quotes by author
static Future<List<QuoteInfo>> byAuthor(String author) async {
final list = await _quotesList();
return list.where((quote) => quote.author == author).toList();
}
/// Returns a random quote by author
static Future<QuoteInfo> singleRandomByAuthor(String author) async {
final list = await _quotesList();
final listByAuthor = list.where((quote) => quote.author == author).toList();
final random = Random();
return listByAuthor[random.nextInt(listByAuthor.length)];
}
}
quotesystring.dart
const quotesyStringJson = '''
[
{
"author": "Abraham Lincoln",
"text": "A house divided against itself cannot stand."
},
... rest of quotes
]
'''
Is suggest making ci/cd pipeline that copies content from quotes.json
to constant string for dart package just like I did.
Because of
await File('quotes.json')
.