'How to manipulate arrays/index

I would like to access the index of a list and concatenate it to the index of another list. For example, the first user's input is "AB" and the second is "CD", so when you press the button it should print "AD" (index 0 from first input + index 1 from second input). I'm getting this error: RangeError (index): Invalid value: Valid value range is empty: 0. I've seen some solutions and many people say that the problem is in trying to access an array that is not ready yet, but I couldn't understand where to put this logic as well as the logic itself.

children: [
              TextField(
                controller: firstTextEditingController,
                decoration: InputDecoration(
                  hintText: 'Insert first input',
                  filled: true,
                  contentPadding: EdgeInsets.all(20),
                ),
                style: TextStyle(color: Colors.black),
              ),
              const SizedBox(
                height: 20,
              ),
              TextField(
                controller: secondTextEditingController,
                decoration: InputDecoration(
                  hintText: 'Insert second input',
                  filled: true,
                  contentPadding: EdgeInsets.all(20),
                ),
                style: TextStyle(color: Colors.black),
              ),
              const SizedBox(
                height: 55,
              ),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                    padding:
                        EdgeInsets.symmetric(horizontal: 50, vertical: 18)),
                onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => NextPage(
                      result: firstTextEditingController.text.length >0 ? firstTextEditingController.text[0] : '' +
                              secondTextEditingController.text.length > 0 ? secondTextEditingController.text[1] : ''),  
                  fullscreenDialog:
                      true,  com o input do user
                )),
                child: Text('Calculate'),
              )
            ],
          ),
        ));
  }
}


Solution 1:[1]

That error means the value of firstTextEditingController.text is empty. You can check whether you have bind it to a textField, and the textField has some text in it.

By the way, the TextEditingController.text is a String(not array). and in dart, a string has operator [] like a List (array like).

Solution 2:[2]

If you have two different TextEditingController() for two inputs, then you dont need array. Because TextEditingController() text is a string and in your code you are

result: firstTextEditingController.text[0] +
        secondTextEditingController.text[1])

trying to access the first index of the both TextEditingController(), and if the text will empty it will throw error.

RangeError (index): Invalid value: Valid value range is empty: 0

This code will work in your case

result: firstTextEditingController.text.length >0 ? firstTextEditingController.text[0] : '' +
    secondTextEditingController.text.lenght > 0 ? secondTextEditingController.text[1] : '')

Complete Solution is this.

    import 'package:flutter/material.dart';

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

   class MyApp extends StatelessWidget {
   @override
   

    Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }

    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final firstTextEditingController = TextEditingController();
      final secondTextEditingController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              TextField(
                controller: firstTextEditingController,
                decoration: InputDecoration(
                  hintText: 'Insert first input',
                  filled: true,
                  contentPadding: EdgeInsets.all(20),
                ),
                style: TextStyle(color: Colors.black),
              ),
              const SizedBox(
                height: 20,
              ),
              TextField(
                controller: secondTextEditingController,
                decoration: InputDecoration(
                  hintText: 'Insert second input',
                  filled: true,
                  contentPadding: EdgeInsets.all(20),
                ),
                style: TextStyle(color: Colors.black),
              ),
              const SizedBox(
                height: 55,
              ),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                    padding:
                    EdgeInsets.symmetric(horizontal: 50, vertical: 18)),
                onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => NextPage(
                        result: ((firstTextEditingController.text.length >0) ?firstTextEditingController.text[0] : '') +
                            ((secondTextEditingController.text.length > 0) ? secondTextEditingController.text[1] : '')),
                    // fullscreenDialog:
                    // true,  com o input do user
                )),
                child: Text('Calculate'),
              )
            ],
          ),
        );
      }
    
      NextPage({String result}) {}
    
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LW001
Solution 2