Home > OS >  Flutter: Undefined name 'uid'
Flutter: Undefined name 'uid'

Time:11-03

class Rapidoreach extends StatefulWidget {
  const Rapidoreach({
    Key? key,
    this.width,
    this.height,
    this.uid,
  }) : super(key: key);

  final double? width;
  final double? height;
  final String? uid;

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



class _RapidoreachState extends State<Rapidoreach> {
  @override
  void initState() {
    RapidoReach.instance.init(apiToken: 'api-key', userId: 'userid');
    RapidoReach.instance.setOnRewardListener(onRapidoReachReward);
    RapidoReach.instance.setRewardCenterClosed(onRapidoReachRewardCenterClosed);
    RapidoReach.instance.setRewardCenterOpened(onRapidoReachRewardCenterOpened);
    RapidoReach.instance
        .setSurveyAvaiableListener(onRapidoReachSurveyAvailable);
    super.initState();
  }

void onRapidoReachReward(num quantity) {
     final DocumentReference docRef = FirebaseFirestore.instance.collection("users").doc(uid); //Here is the error (uid)
        docRef.update({"prizecoins": FieldValue.increment(quantity)});

  }

  void onRapidoReachSurveyAvailable(int? survey) {
    debugPrint('ROR: $survey');
  }

  void onRapidoReachRewardCenterClosed() {
    debugPrint('ROR: closed');
  }

  void onRapidoReachRewardCenterOpened() {
    debugPrint('ROR: opened');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              child: const Text("Launch RapidoReach"),
              onPressed: () => RapidoReach.instance.show(),
            ),
          ],
        )),
      ),
    );
  }
}

Why do I have an issue saying that uid is not defined when is already defined in StatefulWidget?

Can someone help me?

CodePudding user response:

In the state of a StatefulWidget, you can access the instance of the widget through the getter widget, so you need to replace uid with widget.uid:

  void onRapidoReachReward(num quantity) {
    final DocumentReference docRef = 
      FirebaseFirestore.instance.collection("users").doc(widget.uid);
    docRef.update({"prizecoins": FieldValue.increment(quantity)});
  }

CodePudding user response:

you can get the properties from the StatefulWidget inside the State Object by assigning the widget keyword at first :

widget.uid

in your case replace with this :

   FirebaseFirestore.instance.collection("users").doc(widget.uid);
  • Related