0

I have a problem with displaying the string output in Flutter App, I wanted to display the string value in column like this:

I
want
to
split
this

but instead I got this output:

[I, want, to, split, this]

I don't know what to do anymore as I am still new to programming, but I think this code below must be the cause:

void _splitWordInColumn(){
      setState(() {
        sentenceToWord.forEach((e) => print(e));
      });
      }

This is the image of that wrong output and below is my full code:

 import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> sentenceToWord = 'I want to split this'.split(" ");
  
  void _splitWordInColumn(){
  setState(() {
    sentenceToWord.forEach((e) => print(e));
  });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: const Text('Flutter Demo HomePage'),
      ),
      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(sentenceToWord.toString()),
          ],
        ),
      ),
    );
  }
}

I hope someone can help me fix this, Thank you.

2 Answers 2

1

Try to use map operator with the column.

child: Column(
   mainAxisAlignment: MainAxisAlignment.center,
   children: sentenceToWord.map((e) => Text(e)).toList(),
),
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it's worked! But now i wanted to display multiple string with the same method you give and i got an error "Too many positional arguments: 0 expected, but 3 found". i tried to fix it and still got an error since yesterday, would you help me again.
Can you post your error and codes here.
I tried to solve it and i got new error: The argument for the named parameter 'children' was already specified.Try removing one of the named arguments,or correcting one of the names to reference a different named parameter. This is the code. The error at the children word, how to solve it so that i can display more. children: sentenceToWord.map((e) => Text(e)).toList(), children: sentenceToWord2.map((e) => Text(e)).toList(), children: sentenceToWord2.map((e) => Text(e)).toList(),
You nesting the Columns like this pastebin.com/MyGyZ4Ff (click in the url) but this is not the good practice. If your data is huge then I would recommend you to use ListView.Builder.
0

Try this :

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String sentenceToWord = 'I \nwant \nto \nsplit \nthis';

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: const Text('Flutter Demo HomePage'),
      ),
      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(sentenceToWord),
          ],
        ),
      ),
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.