-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Closed
flutter/plugins
#1132Labels
p: shared_preferencesPlugin to read and write Shared PreferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.waiting for PR to land (fixed)A fix is in flightA fix is in flight
Description
Hello,
I've got issue with double when application restart (only after app restart) double values change.
Example :
I save/persist 1.45, and when I restart I get : 1.4500000476837158
I save/persist 1.4 and when I restart I get : 1,399999976158142
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@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> {
int _counter = 0;
double _value;
@override
void initState() {
// TODO: implement initState
super.initState();
loadPref();
}
void loadPref () async{
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
final v = prefs.getDouble('valueToTest');
if (v == null){
_value = 1.45;
prefs.setDouble('valueToTest', _value);
}else {
_value = v;
}
});
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Double value $_value'),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}Metadata
Metadata
Assignees
Labels
p: shared_preferencesPlugin to read and write Shared PreferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.waiting for PR to land (fixed)A fix is in flightA fix is in flight
