4

I'm trying to create a very simple app where an element from a list gets displayed when the center widget is clicked.

This is my code:

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
      MaterialApp(home: MyApp()),
    );

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(),
      ),
    );
  }
}

class picker extends StatefulWidget {
  @override
  _pickerState createState() => _pickerState();
}

class _pickerState extends State<picker> {
  List yourList = ["Yes", "No", "Maybe"];
  int randomIndex;

  _pickerState() {
    int randomIndex = Random().nextInt(yourList.length);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
        onPressed: () {
          setState(() {
            print(yourList[randomIndex]);
          });
        },
        child: Text('$randomIndex'),
      ),
    );
  }
}

There are no errors shown but at the same time, my objective isn't reached since no text is shown, and nothing is printed in the terminal even if I've added this code print(yourList[randomIndex]);. How do I display the text and make the print appear?

enter image description here

1
  • i can't see where you navigate from class MyApp to class picker? If you just put a Center() in body of class MyApp, it'll never change since it doesn't navigate to anywhere Commented Oct 21, 2020 at 1:11

3 Answers 3

2

Another solution is to set

randomIndex = Random().nextInt(yourList.length);

in setState(() { like below.

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(
      MaterialApp(home: MyApp()),
    );

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: picker(),
      ),
    );
  }
}

class picker extends StatefulWidget {
  @override
  _pickerState createState() => _pickerState();
}

class _pickerState extends State<picker> {
  List yourList = ["Yes", "No", "Maybe"];
  int randomIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextButton(
        onPressed: () {
          setState(() {
            randomIndex = Random().nextInt(yourList.length);
          });
        },
        child: Text(yourList[randomIndex]),
      ),
    );
  }
}

To have a starting text the setState needs to be taken out in a separate function like:

class _pickerState extends State<picker> {
 List yourList = ["Yes", "No", "Maybe"];
 String buttonText = "Initial Text";
 int randomIndex = 0;
 
 void _newRandomIndex() {
   setState(() {
     randomIndex = Random().nextInt(yourList.length);
     buttonText = yourList[randomIndex];
   });
 }

 @override
 Widget build(BuildContext context) {
   return Center(
     child: TextButton(
       onPressed: _newRandomIndex,
       child: Text(buttonText),
     ),
   );
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Best answer ever. BTW, do you know how to only start the randomization upon clicking? Is it possible to initialize the center text with my own custom text like "Ask Anything" and only have either yes, no, or maybe appear when clicked.
1

Two mistakes:

  1. You were not assigning value to the variable you actually wanted to use here:
  int randomIndex;

  _pickerState() {
    // you declared a local variable here and you were using value of variable you declared on top
    int randomIndex = Random().nextInt(yourList.length);
  }
  1. The method _pickerState() was not called and that's why there was null value in the TextButton.

Here's the working code, you can simply copy paste it:

class _PickerState extends State<Picker> {
  List yourList = ["Yes", "No", "Maybe"];
  int randomIndex;

  _pickerState() {
    randomIndex = Random().nextInt(yourList.length);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () {
            _pickerState();
            setState(() {
              print(yourList[randomIndex]);
            });
          },
          child: Text('$randomIndex'),
        ),
      ),
    );
  }
}

Quick Tip: Try to use UpperCase for naming the class or any CustomWidget its more convenient way

Comments

0

Try using this.randomIndex = Random().nextInt(yourList.length); instead when initializing a variable in a constructor body.

Otherwise, you are declaring a different local variable when the class is initialized.

Next time try debugging this error by setting a default value outside of initialization to check if there is a similar problem such as your initialization not going through to your state.

I'd also suggest that you change your class names to CamelCase to avoid confusion and so people can see that that is code for when the class initializes. Here is a dart article on styling code.

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.