Reputation: 381
Here is my code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget{
@override
State createState() => Comeback();
}
class Comeback extends State<MyApp>{
Widget hello(BuildContext context){
var listInsults = ['no you', 'but this applies to you more than me', 'that insult was horrible'];
var finalVar = listInsults.toList()..shuffle();
return Column(
children: <Widget>[
Row(
children: <Widget>[
CircleAvatar(
child: Text(
"XD"
)
),
Text(
finalVar[0]
)
]
)
],
);
}
Widget build(BuildContext context){
var listy = 0;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(
"hi"
)
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) => hello(context),
itemCount: listy
),
),
Row(
children: <Widget>[
Expanded(
child: TextFormField(
decoration: InputDecoration(
hintText: "Insult me plz!"
)
),
),
RaisedButton(
onPressed: () {
setState(() {
listy++;
//where program is being printed
print(listy);
});
},
child: Icon(Icons.add_circle_outline),
highlightColor: Colors.amber[600]),
],
)
],
)
)
);
}
}
Basically, I am trying to make a comeback generator where the user insults the program via textbox and the program roasts them back via ListView.builder. Unfortunately, when testing my code, the program's insults are not being shown. As you can see below the comment in the code, I tried to see what was happening to the variable listy(used for changing the number of items in the list) and realized that it is only being updated once. Here is the terminal:
I/flutter ( 4632): 1
I/flutter ( 4632): 1
I/flutter ( 4632): 1
I/flutter ( 4632): 1
I/chatty ( 4632): uid=10091(com.example.comeback_generator) Thread-2 identical 2 lines
I/flutter ( 4632): 1
I/flutter ( 4632): 1
I/flutter ( 4632): 1
I/flutter ( 4632): 1
I/chatty ( 4632): uid=10091(com.example.comeback_generator) Thread-2 identical 1 line
I/flutter ( 4632): 1
When looking above, you can see that the program is clearly not updating the variable listy. If it were, the numbers printed would have grown by one. I've tried copy and pasting code from a similar project that worked and modified it by changing the variables and removing unnecessary code.
Here is the code I copied and pasted:
onPressed: () {
if(!products.contains(myController.text.toLowerCase()) && !products.contains(myController.text.toUpperCase()) && !products.contains(myController.text)) {
setState(() {
products.add(myController.text);
_save();
_read();
});
}
},
and here is how I modified it:
setState(() {
listy++;
print(listy);
});
After modifying it the program still had the same bug.
What is wrong with my program? Why is the program wrong? How can I fix this and avoid mistakes and inconsistencies like this in the future?
Keep in mind that I am a beginner to flutter, and I am prone to mistakes and error.
Upvotes: 0
Views: 2165
Reputation: 103351
That's because every time you refresh the widget, the method build
is called then your variable listy is assigned to 0 again.
Widget build(BuildContext context){
var listy = 0;
To fix your issue, just move the declaration outside your build
method, like this:
var listy = 0;
Widget build(BuildContext context){
...
Upvotes: 1