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?
