Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work in flutter #120

Open
orl0pl opened this issue Oct 7, 2024 · 1 comment
Open

Doesn't work in flutter #120

orl0pl opened this issue Oct 7, 2024 · 1 comment
Labels

Comments

@orl0pl
Copy link

orl0pl commented Oct 7, 2024

Because of await File('quotes.json').

@nelsonic nelsonic added help wanted Extra attention is needed technical user-feedback labels Oct 8, 2024
@orl0pl
Copy link
Author

orl0pl commented Oct 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants