Home > Blockchain >  Flutter: String parameter is not working?
Flutter: String parameter is not working?

Time:01-27

I'm trying to pass the String parameter to the widgets but unfortunately it's not working and i don't know how is that happening because i have worked on the same parameter in another widget and it has worked :'(.

Here is my code:

MainPage.dart

//---------------------------------------------------------
//Assigned String from here and it works with the widgets except: MainHydrationProgressPage
//---------------------------------------------------------
late final String? tanksID = ModalRoute.of(context)?.settings.arguments as String?;


  late final _pages = <Widget>[
        MainTankHydrationPoolPage(tankID: tanksID,),
        MainHydrationProgressPage(tanksID: tanksID,),
        SummaryPage(tanksID: tanksID),
      ];

MainHydrationProgressPage.dart

import 'package:flutter/material.dart';
import 'package:smart_tank1/main_tank_detail_ui/hydration_progress/progress_view.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_locales/flutter_locales.dart';


class MainHydrationProgressPage extends StatefulWidget {
//-----------------
//Here is the String that i want to work on
//-----------------

  final String? tanksID;
  MainHydrationProgressPage({Key? key, required this.tanksID});

  static const routeName = 'progress-screen';

  @override
  _MainHydrationProgressPageState createState() => _MainHydrationProgressPageState();
}

class _MainHydrationProgressPageState extends State<MainHydrationProgressPage> {
 bool onAndOff = false;
 late final dbRef = FirebaseDatabase.instance;

 //final fStore = FirebaseFirestore.instance;
@override
void initState(){
  
  dbRef;
  //fStore;
  super.initState();
}

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.only(bottom: 136, top: 32.0),
        child: Column(
          children: [
            SizedBox(width: double.infinity),
            LocaleText(
              "waterPercentage",
              style: Theme.of(context).textTheme.headline4,
            ),
            Expanded(
//-------------------------------------------------------------------------------------------
//Trying to assign tanksID here but it's not working it keeps showing the error line under it
//-------------------------------------------------------------------------------------------
              child: ProgressView(ID: tanksID),
            ),
            SizedBox(height: 10,),
            Padding(
              padding: const EdgeInsets.only(left: 20.0, right: 10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  LocaleText('pump', style: TextStyle(fontSize: 20),),               
                  buildSwitch(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

   Widget buildSwitch() => Transform.scale(
        scale: 1.5,
        child: Switch.adaptive(
          activeColor: Colors.blueAccent,
          activeTrackColor: Colors.blue.withOpacity(0.4),
          inactiveThumbColor: Colors.white,
          inactiveTrackColor: Colors.grey,
          splashRadius: 50,
          value: onAndOff,
          onChanged: (value){
          final user = FirebaseAuth.instance.currentUser;
           setState(() {
               onAndOff = value;
                if (value) {
                  //--------------
                  //1. Firestore
                  //--------------
                  // fStore.collection('esp').doc(user.uid).update({
                  //   'pump': 'on',
                  // });
                  //-----------------
                  //2. Realtime database
                  //-----------------
                  dbRef.ref().child('users'). child(user!.uid).child('pump').set('on').asStream();                 
                }else {
                  //--------------
                  //1. Firestore
                  //--------------
                  // fStore.collection('esp').doc(user.uid).update({
                  //   'pump': 'off',
                  // });

                  //-----------------
                  //2. Realtime database
                  //-----------------
                   dbRef.ref().child('users').child(user!.uid).child('pump').set('off').asStream();
                                            
              }
          },           
           );       
        }
        ),
      );
          }

Here is what it tells me: Undefined name 'tanksID'. Try correcting the name to one that is defined, or defining the name.

Anyone faced this problem before? and how can it be solved! I did flutter clean and flutter get packages then restarted vscode but it didn't work! :'( please help

CodePudding user response:

It worked with you before because maybe you were using a stateless widget or the variable defined in the state itself, but in this case you have a stateful widget and the variable is defined in the stateful widget not in the state itself, so you have to call it like: widget.tanksID

  •  Tags:  
  • Related