Skip to content

Instantly share code, notes, and snippets.

@patientplatypus
Created March 6, 2018 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patientplatypus/bf3950a5cf93d62b9e91157a7850459d to your computer and use it in GitHub Desktop.
Save patientplatypus/bf3950a5cf93d62b9e91157a7850459d to your computer and use it in GitHub Desktop.
Having trouble updating passed variable in flutter
class NavigationState extends State<MyNavigationHolder> {
var goToPass = "/page1";
assignPass(page){
print("inside assignPass");
print("value of page: " + page);
setState(() {goToPass = page;});
}
//button gets pressed and assignPass updates goToPass with new page val
//then I call HeaderPage
new Flexible(
child: new HeaderPage(goTo: goToPass)
),
//Inside header page:
import 'package:flutter/material.dart';
import 'main.dart';
class HeaderPage extends StatefulWidget {
const HeaderPage({
Key key,
this.goTo
}) : super(key: key);
final String goTo;
@override
HeaderPageState createState() => new HeaderPageState(goTo);
}
class HeaderPageState extends State<HeaderPage> {
HeaderPageState(this.goTo);
final String goTo;
String goToPass;
sunPressed(){
}
moonPressed(){
}
@override
void initState() {
// TODO: implement initState
super.initState();
setState(() {goToPass = goTo;});
}
@override
Widget build(BuildContext context) {
print("value of goTo in Widget build: " + goToPass);
Widget ButtonGrad(){
print("value of goTo: " + goToPass);
if (goToPass=="/page3"){
//magic happens
//i only want to fire buttongrad if goToPass="/page3")
//PROBLEM: goToPass is always == "/page1" and I don't know why;
//Output from console:
Performing full restart...
Restarted app in 2,570ms.
I/flutter ( 5550): value of goTo in Widget build: /page1
I/flutter ( 5550): value of goTo: /page1
I/flutter ( 5550): inside assignPass
I/flutter ( 5550): value of page: /page2
I/flutter ( 5550): value of goTo in Widget build: /page1
I/flutter ( 5550): value of goTo: /page1
I/flutter ( 5550): inside assignPass
I/flutter ( 5550): value of page: /page4
I/flutter ( 5550): value of goTo in Widget build: /page1
I/flutter ( 5550): value of goTo: /page1
I/flutter ( 5550): inside assignPass
I/flutter ( 5550): value of page: /page1
I/flutter ( 5550): value of goTo in Widget build: /page1
I/flutter ( 5550): value of goTo: /page1
I/flutter ( 5550): inside assignPass
I/flutter ( 5550): value of page: /page3
I/flutter ( 5550): value of goTo in Widget build: /page1
I/flutter ( 5550): value of goTo: /page1
I/flutter ( 5550): inside getActivityList
//What am I doing wrong?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment