Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:16:18+00:00 2026-06-15T07:16:18+00:00

From the API page , I gather there’s no function for what I’m trying

  • 0

From the API page, I gather there’s no function for what I’m trying to do. I want to read text from a file storing it as a list of strings, manipulate the text, and save the file. The first part is easy using the function:

abstract List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8])

However, there is no function that let’s me write the contents of the list directly to the file e.g.

abstract void writeAsLinesSync(List<String> contents, [Encoding encoding = Encoding.UTF_8, FileMode mode = FileMode.WRITE])

Instead, I’ve been using:

abstract void writeAsStringSync(String contents, [Encoding encoding = Encoding.UTF_8, FileMode mode = FileMode.WRITE])

by reducing the list to a single string. I’m sure I could also use a for loop and feed to a stream line by line. I was wondering two things:

  1. Is there a way to just hand the file a list of strings for writing?
  2. Why is there a readAsLinesSync but no writeAsLinesSync? Is this an oversight or a design decision?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:16:20+00:00Added an answer on June 15, 2026 at 7:16 am

    I just made my own export class that handles writes to a file or for sending the data to a websocket.

    Usage:

    exportToWeb(mapOrList, 'local', 8080);
    exportToFile(mapOrList, 'local/data/data.txt');
    

    Class:

    //Save data to a file.
    void exportToFile(var data, String filename) =>
        new _Export(data).toFile(filename);
    
    //Send data to a websocket.
    void exportToWeb(var data, String host, int port) =>
        new _Export(data).toWeb(host, port);
    
    class _Export {
    
      HashMap mapData;
      List listData;
      bool isMap = false;
      bool isComplex = false;
    
      _Export(var data) {
        // Check is input is List of Map data structure.
        if (data.runtimeType == HashMap) {
          isMap = true;
          mapData = data;
        } else if (data.runtimeType == List) {
          listData = data;
          if (data.every((element) => element is Complex)) {
            isComplex = true;
          }
        } else {
          throw new ArgumentError("input data is not valid.");
        }
      }
    
      // Save to a file using an IOSink.  Handles Map, List and List<Complex>.   
      void toFile(String filename) {
        List<String> tokens = filename.split(new RegExp(r'\.(?=[^.]+$)'));
        if (tokens.length == 1) tokens.add('txt');
        if (isMap) {
          mapData.forEach((k, v) {
            File fileHandle = new File('${tokens[0]}_k$k.${tokens[1]}');
            IOSink dataFile = fileHandle.openWrite();
            for (var i = 0; i < mapData[k].length; i++) {
              dataFile.write('${mapData[k][i].real}\t'
                  '${mapData[k][i].imag}\n');
            }
            dataFile.close();
          });
        } else {
          File fileHandle = new File('${tokens[0]}_data.${tokens[1]}');
          IOSink dataFile = fileHandle.openWrite();
          if (isComplex) {
            for (var i = 0; i < listData.length; i++) {
              listData[i] = listData[i].cround2;
              dataFile.write("${listData[i].real}\t${listData[i].imag}\n");
            }
          } else {
            for (var i = 0; i < listData.length; i++) {
              dataFile.write('${listData[i]}\n');
            }
          }
          dataFile.close();
        }
      }
    
      // Set up a websocket to send data to a client.
      void toWeb(String host, int port) {
        //connect with ws://localhost:8080/ws
        //for echo - http://www.websocket.org/echo.html
        if (host == 'local') host = '127.0.0.1';
        HttpServer.bind(host, port).then((server) {
          server.transform(new WebSocketTransformer()).listen((WebSocket webSocket) {
            webSocket.listen((message) {
              var msg = json.parse(message);
              print("Received the following message: \n"
                    "${msg["request"]}\n${msg["date"]}");
                if (isMap) {
                  webSocket.send(json.stringify(mapData));
                } else {
                  if (isComplex) {
                    List real = new List(listData.length);
                    List imag = new List(listData.length);
                    for (var i = 0; i < listData.length; i++) {
                      listData[i] = listData[i].cround2;
                      real[i] = listData[i].real;
                      imag[i] = listData[i].imag;
                    }
                    webSocket.send(json.stringify({"real": real, "imag": imag}));
                  } else {
                    webSocket.send(json.stringify({"real": listData, "imag": null}));
                  }
                }
            },
            onDone: () {
                print('Connection closed by client: Status - ${webSocket.closeCode}'
                    ' : Reason - ${webSocket.closeReason}');
                server.close();
            });
          });
        });
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to reload a page after the response from the facebook api. With
My app uses an API from an external JAR file. This JAR file has
I want to call a Subversion API from a Visual Studio 2003 C++ project.
I am trying to use the Streaming API from php, I use the code
I cannot find anything on the api page from google, is this even possible?
I have already read the Instapaper API page , but it only explains how
From REST API of twitter I user get/followers function. I pasted a code snippet
I am trying to pull data from api cart but I am not experienced
I copied this example from api.jquery.com $('.target').change(function() { alert('Handler for .change() called.'); }); I
I would like to implement a switch button, android.widget.Switch (available from API v.14). <Switch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.