-1

App Screenshot

How I got data from the txt file.

Future<String>? get textAsString async {
  Uri? uri = Uri.tryParse(text.url);
  if (uri != null) {
    String text = await http.read(uri);
    return text;
  }
  return '';
}

My widget structure and code layout.

FutureBuilder<String>(
  future: currentScene.textAsString,
  builder: (context, snapshot) {
    String? text = snapshot.data;
    if (snapshot.hasData && text != null) {
      return ListView(
        padding: kAppPadding,
        controller: _controller,
        children: [
         Text(
           text,
           style: TextStyle(
             height: 1.8,
             fontFamily: 'Roboto',
             color: kWhiteColor,
             fontWeight: FontWeight.w300,
             fontSize: 17,
           ),
         ),
        ],
      );
          } else if (snapshot.hasError) {
            return Center(
              child: AppErrorText(
                onPressed: () {},
              ),
            );
          } else {
            return Center(
              child: AppProgressIndicator(),
            );
          }
        })

I have a TXT url stored in cloud storage and I want to retrieve the text and create a text reader app.

I used http.read(uri) to get the content of the TXT file and passed the String to a Text Widget wrapped with a FutureBuilder

I noticed the String contained some weird characters (â)... so I looking for a way to remove/replace those characters.

Rezia
  • 35
  • 1
  • 6
  • What all characters do you categorise as `special` ? – Nisanth Reddy May 17 '21 at 04:34
  • I suggest it is the way you are displaying them that is wrong. You don't need external libraries for that. – user207421 May 17 '21 at 04:42
  • @NisanthReddy sorry for the late reply...If you take a look at the App Screenshot you can see some weird characters like (â). – Rezia May 17 '21 at 11:10
  • @user207421 I just passed the string to a Text Widget wrapped with a Future Builder. – Rezia May 17 '21 at 11:17
  • @Rezia There must be something wrong in your widget code or with the way to fetch and process the data. Update your question with more details of your code, since this is not normal. – Nisanth Reddy May 17 '21 at 11:50
  • Hmm, I am pretty sure this is due to incorrect headers. So, post your `uri` once so that we can check it's encoding. – Nisanth Reddy May 17 '21 at 12:26
  • @NisanthReddy TXT Url - https://firebasestorage.googleapis.com/v0/b/fantasyapp-c636c.appspot.com/o/AppContent%2FScenes%2F0Hmv9ZZL2tunZAQh%2FScene%202.txt?alt=media&token=48fe5c72-9584-4c52-941e-b9c5859b9479 Type - application/octet-stream – Rezia May 17 '21 at 13:49
  • @Rezia I have added an answer. Check it out if that works for you. – Nisanth Reddy May 17 '21 at 14:19

2 Answers2

0

I am not sure about external library but you can use ASCII code for identify spacial character. Here is the ASCII number for spacial character

ASCII = 32 to 47 // 32 is a code for blank space
ASCII = 58 to 64 
ASCII = 91 to 96 
ASCII = 123 to 126

in flutter you can get ASCII value of string as follow

 String _string = 'Hello @World#';
 List<int> asciiList = _string.codeUnits;
 String newString = _string;

 for (int i = 0; i < asciiList.length; i++) {
 if (asciiList[i] >= 33 && asciiList[i] <= 47 ||  //we don't want to remove blank space so we start from 33
     asciiList[i] >= 58 && asciiList[i] <= 64 ||
     asciiList[i] >= 91 && asciiList[i] <= 96 ||
     asciiList[i] >= 123 && asciiList[i] <= 126) {
        newString = newString.replaceAll(_string[i], '');
      } else {
         print("It's not a spacial character");
        }
     }

    print(newString);  //Hello world
Hemal Moradiya
  • 1,715
  • 1
  • 6
  • 26
0

As suspected by our discussion in the comments, this is due to type encoding.

Since, it is .txt file and not a String on your server. You need to use a Decoder to get the data in the correct format.

Use it like this,

Add these imports,

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

Then change your textAsString function

Future<String>? get textAsString async {
    Uri uri = Uri.parse('https://firebasestorage.googleapis.com/v0/b/fantasyapp-c636c.appspot.com/o/AppContent%2FScenes%2F0Hmv9ZZL2tunZAQh%2FScene%202.txt?alt=media&token=48fe5c72-9584-4c52-941e-b9c5859b9479');
    return await new HttpClient()
        .getUrl(uri) // Reads the data
        .then((HttpClientRequest request) => request.close()) // Then closes the request to return us the Future of response
        .then((HttpClientResponse response) => 
            response.transform(new Utf8Decoder()).join()); // transforms the response using UTF-8 
}

enter image description here

Nisanth Reddy
  • 5,967
  • 1
  • 11
  • 29