Home > other >  Get content from txt file and put it in a list Flutter/Dart
Get content from txt file and put it in a list Flutter/Dart

Time:04-12

this is my first question here on stackoverflow. Before i get into the question itself, i want to point out that i'm a beginner on Flutter and Dart and have no previous experience in mobile development.

Now, as the title says i'm having some issues while trying to extract the content of a text file and store the individual names inside the indexes of a list. The end goal of my application is to print a never ending list of these names, each separated to another by a comma. For example:

top model,
torcitóre,
torcolière,
tornitóre,
tosacàni,
tossicòlogo,
tour operator,
tracciatóre,
tranvière,
trattorìsta,
trattóre²,
trebbiatóre,
trecciaiòlo,
trevière,
tributarìsta,
trinciatóre,
trivellatóre,
truccatóre,
tubìsta,
turnìsta,

The text file, named jobs.txt contains more than a 1000 italian job names. After several hours searching, all i have here is this (got from another user):


import 'dart:async';
import 'dart:convert';
import 'dart:io';

void main(List<String> arguments) {
  try {
    final _jobs = File('jobs.txt');

    List<String> lines = _jobs.readAsLinesSync(encoding: ascii);
    for (var l in lines) print(l);
  } catch (Exception) {
    print(Exception.toString());
    
  }
}

jobs.txt is inside the same directory as the dart file, which is bin.

While trying to give the _jobs variable the relative path:

FileSystemException: Cannot open file, path = 'binjobs.txt' (OS Error: Impossibile trovare il file specificato.
, errno = 2)

With absolute path:

FileSystemException: Cannot open file, path = 'C:UsersUserDesktopdartdart_application_injobs.txt' (OS Error: La sintassi del nome del file, della directory o del volume non è corretta.
, errno = 123)

For reasons i don't know, if that even matters, in the path specified in the error message, the \ look like they don't even exist.

By the way, this is my project structure: project structure

If your solution includes the usage of the Future class or async-await keywords, i'll be thankful if you could explain me how they work.

UPDATE: Look in the comments for the full solution to this issue. The verified answer isn't the full solution.

CodePudding user response:

Create an assets folder in your project, in your pubspec.yaml specify the asset (make sure it's in your assets folder, that the file exists and is readable)

flutter:
  assets:
    - assets/jobs.txt

Now you can access your text file like so -

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/jobs.txt');
}

When the function returns your text, you can split it by commas and loop through all the jobs as you wish

CodePudding user response:

try using rootBundle

  pubspec.yaml
      assets:
        - text_file/

  import 'package:flutter/services.dart' show rootBundle;

  Future<String> getData() async {
    try {
      return await rootBundle.loadString('text_file/four_words.txt');
    } catch (e) {
      throw (e.toString());
    }
  }
  • Related