OnBoardingScreens: Do not call setState in the constructor

Even though it is called with a future, it seems to provoke an
exception. My flutter skills are not good enough to know why this is not
okay.
This commit is contained in:
Vishesh Handa
2019-01-11 18:48:51 +01:00
parent 839da00307
commit 19ae26cadc

View File

@ -22,6 +22,8 @@ class OnBoardingScreenState extends State<OnBoardingScreen> {
var pageController = PageController(); var pageController = PageController();
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
String publicKey = "Generating ...";
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var pageCount = 1; var pageCount = 1;
@ -45,6 +47,7 @@ class OnBoardingScreenState extends State<OnBoardingScreen> {
SharedPreferences.getInstance().then((SharedPreferences pref) { SharedPreferences.getInstance().then((SharedPreferences pref) {
pref.setString("sshCloneUrl", sshUrl); pref.setString("sshCloneUrl", sshUrl);
this._generateSshKey();
}); });
}); });
} }
@ -57,7 +60,7 @@ class OnBoardingScreenState extends State<OnBoardingScreen> {
curve: Curves.easeIn, curve: Curves.easeIn,
); );
}, },
scaffoldKey: _scaffoldKey, publicKey: publicKey,
); );
} }
@ -92,6 +95,22 @@ class OnBoardingScreenState extends State<OnBoardingScreen> {
this._pageSshKeyDone = true; this._pageSshKeyDone = true;
}); });
} }
void _generateSshKey() {
generateSSHKeys(comment: "GitJournal").then((String _publicKey) {
setState(() {
print("Changing the state");
publicKey = _publicKey;
Clipboard.setData(ClipboardData(text: publicKey));
var text = "Public Key copied to Clipboard";
this
._scaffoldKey
.currentState
.showSnackBar(new SnackBar(content: new Text(text)));
});
});
}
} }
class OnBoardingGitUrl extends StatefulWidget { class OnBoardingGitUrl extends StatefulWidget {
@ -184,42 +203,15 @@ class OnBoardingGitUrlState extends State<OnBoardingGitUrl> {
} }
} }
class OnBoardingSshKey extends StatefulWidget { class OnBoardingSshKey extends StatelessWidget {
final Function doneFunction; final Function doneFunction;
final GlobalKey<ScaffoldState> scaffoldKey; final String publicKey;
OnBoardingSshKey({ OnBoardingSshKey({
@required this.doneFunction, @required this.doneFunction,
@required this.scaffoldKey, @required this.publicKey,
}); });
@override
OnBoardingSshKeyState createState() {
return new OnBoardingSshKeyState();
}
}
class OnBoardingSshKeyState extends State<OnBoardingSshKey> {
String publicKey = "Generating ...";
void initState() {
super.initState();
generateSSHKeys(comment: "GitJournal").then((String _publicKey) {
setState(() {
print("Changing the state");
publicKey = _publicKey;
Clipboard.setData(ClipboardData(text: publicKey));
var text = "Public Key copied to Clipboard";
this
.widget
.scaffoldKey
.currentState
.showSnackBar(new SnackBar(content: new Text(text)));
});
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
@ -239,7 +231,7 @@ class OnBoardingSshKeyState extends State<OnBoardingSshKey> {
), ),
RaisedButton( RaisedButton(
child: Text("Start Clone"), child: Text("Start Clone"),
onPressed: this.widget.doneFunction, onPressed: this.doneFunction,
) )
], ],
); );