Skip to content

Instantly share code, notes, and snippets.

@stegrams
Last active May 12, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stegrams/953a6104fc471d164c8d421c005871b5 to your computer and use it in GitHub Desktop.
Save stegrams/953a6104fc471d164c8d421c005871b5 to your computer and use it in GitHub Desktop.
Funny state behavior that I can't reproduce
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<int> productQtys = [10];
String status = 'Status:\n';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: Center(
child: Text(
status,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
int index = 0;
// print(productQtys);
status += '\n$productQtys';
if (productQtys[index] != 1) {
productQtys[index] = productQtys[index] - 1;
}
// print(index);
status += '\n$index';
// print(productQtys);
status += '\n$productQtys';
});
},
tooltip: 'Decrement',
child: Icon(Icons.remove),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment