0

in my project I want to add the textinput from a Textfield into a List to build a Listview.builder. The Problem is that I dont know how to add the String to the List (I want to use the string like in a todo app to make serval dates). (for example) time.add(time1) isnt working and I hope someone can help me. Is there a completly other way to transport the Inputtext to the list, Im open for everything

First Page

class Homescreen extends StatefulWidget {
String time1;
String who1;
String where1;
String when1;
Homescreen({this.time1, this.who1, this.where1, this.when1});

@override
_HomescreenState createState() => _HomescreenState();
}




TextEditingController myControllertime = TextEditingController();
TextEditingController myControllerwho = TextEditingController();
TextEditingController myControllerwhen = TextEditingController();
TextEditingController myControllerwhere = TextEditingController();

class _HomescreenState extends State<Homescreen> {
List<String> time = ["8:00",];
List<String> who = ["Eric", ];
List<String> when = ["Friday 21.4.21",];
List<String> where = ["At McDonalds", ];

 ListView.builder(
                   physics: NeverScrollableScrollPhysics(),
                   shrinkWrap: true,
                   itemCount: where.length,
                   itemBuilder: (BuildContext context, int Index) {
                     return Column(children: [
                       SizedBox(
                         height: 40,
                       ),
                       Container(
                           child: GestureDetector(
                               onTap: () {
                                 Navigator.push(
                                     context,
                                     MaterialPageRoute(
                                         builder: (context) => Meet1()));
                               },
                               child: Container(
                                   width: size.width * 0.9,
                                   decoration: BoxDecoration(
                                     borderRadius: BorderRadius.all(
                                         Radius.circular(70)),
                                     gradient: LinearGradient(
                                       begin: Alignment.topRight,
                                       end: Alignment.bottomRight,
                                       colors: [
                                         Colors.orange,
                                         Colors.purple,
                                       ],
                                     ),
                                   ),
                                   child: Column(children: <Widget>[
                                     SizedBox(
                                       height: 10,
                                     ),
                                     Padding(
                                       padding: EdgeInsets.all(20),
                                       child: Column(
                                         children: <Widget>[
                                           Text(
                                             time[Index],
                                             style: TextStyle(
                                                 color: Colors.white,
                                                 fontSize: 40,
                                                 fontWeight:
                                                     FontWeight.bold),
                                           ),
                                           SizedBox(
                                             height: 10,
                                           ),
                                           Text(
                                             who[Index],
                                             style: TextStyle(
                                                 color: Colors.white,
                                                 fontSize: 20,
                                                 fontWeight:
                                                     FontWeight.bold),
                                           ),
                                           Text(
                                             when[Index],
                                             style: TextStyle(
                                                 color: Colors.white,
                                                 fontSize: 20,
                                                 fontWeight:
                                                     FontWeight.bold),
                                           ),
                                           Text(
                                             where[Index],
                                             style: TextStyle(
                                                 color: Colors.white,
                                                 fontSize: 20,
                                                 fontWeight:
                                                     FontWeight.bold),
                                           ),

Second Page

TextButton(
          child: Icon(
            Icons.check_circle_outline_rounded,
            color: Colors.green,
            size: 120,
          ),
          onPressed: () {
            Navigator.pop(context, MaterialPageRoute(builder: (builder) {
              return Homescreen(
              time1: myControllertime.text,
              who1: myControllerwho.text,
              when1: myControllerwhen.text,
              where1: myControllerwhere.text
              ,
              );
            }));
          })
      ],
    ));




child: Column(children: <Widget>[
      SizedBox(
        height: 10,
      ),
      Padding(
        padding: EdgeInsets.all(25),
        child: Column(
          children: <Widget>[
            TextField(
                controller: myControllertime,
                decoration: InputDecoration(hintText: " Time ")),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: myControllerwho,
              decoration: InputDecoration(hintText: " Who "),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: myControllerwhen,
              decoration: InputDecoration(hintText: " Date "),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: myControllerwhere,
              decoration: InputDecoration(hintText: " Where "),
            ),
            
1
  • //void main() { String m = "map"; List mx = ["map1","2","3"]; mx.add(m); print(mx); } // This is a bad example of adding a string to a list, but why I am commenting this? this will do the job,you can use this to your job too, but this is not the right way. Commented Mar 11, 2021 at 12:08

1 Answer 1

1

There is too much wrong tbh in the code. Let us help you.

Firstly, time.add(time1) is not working because you are everytime creating New HomeScreen after addition and List time is reinitialised again and again. thus adding a value wont work here.

To save data you have to actually put that data somewhere in another class with static reference or may be persist them using sharedprefs/anything but that different case.

for example you can create a class like this

class TODOData{
 static List<String> time = ["8:00",];
 static List<String> who = ["Eric", ];
 static List<String> when = ["Friday 21.4.21",];
 static List<String> where = ["At McDonalds", ];
}

Now whenever you want to save new field for example time1, in your case, just use it TODOData.time.add(time1); You don't need to pass it your home screen. And you can access that data using TODOData.time / TODOData.who etc.

You can now even remove all those fields time1, etc from HomeScreen Widget. You can add all those values in onPressed method in SecondScreen in the list as mentioned above. and navigate to HomeScreen, it will have that new data.

This will solve your problem temporarily for data addition.

You can remove all those lists from _HomescreenState and use as mentioned above.

Now comes the ideal way.

You should always create a model class to simplyfy that data. It makes things more readable, accesable and scalable on large projects.

For example instead of creating 4 different list of data you can actually create a model class like this.

class TODOModel{(
 String time,
 String who,
 String when,
 String where
)}

And then create a list of it in the same TODOData class.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your help. I put the ToDoData and Todolist in the main.dart file and on the secondscreen I tipped: on pressed ... ToDoData.time.add(myControllertime.text). But on my Homescreen I dont know how to use the Lists. For example Text(time[index]), the programm cant find the list time and the others. Dou you know what I done false
class TimescheduleData { static List<String> time = [ ]; static List<String> who = [ "Eric", ]; static List<String> when = [ "Friday 21.4.21", ]; static List<String> where = [ "At McDonalds", ]; } class Timeschedulemodel { String time; String who; String when; String where; }
onPressed: () { TimescheduleData.time.add(myControllertime.text); TimescheduleData.who.add(myControllerwho.text); TimescheduleData.when.add(myControllerwhen.text); TimescheduleData.where.add(myControllerwhere.text); Navigator.pop(
Text( time[Index],) #time isnt working
@EricPhilippi you have to use Text( TimescheduleData.time[Index],) instead
|

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.