The method ‘ref’ isn’t defined for the type ‘BuildContext’

Issue

This Content is from Stack Overflow. Question asked by Usama Bin Tahir

I am trying to use the onchange value of textfield and pass it to the next screen. While using the displaying the onchange value using the riverpod provider it shows the following error message. I have imported the riverpod library, also tried using ref.read, not working out. Kindly if someone could help me out.

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod/riverpod.dart';

class Screen1 extends StatefulWidget {
   Screen1({Key? key}) : super(key: key);

  @override
  State<Screen1> createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {
  late String text;

  late String Example;

  late final screenProvider = Provider<String>((ref) => text);
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: const Center(
          child:  Text(
            'Screen1',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children:  <Widget>[
            TextField(
              onChanged: (value){
                text = value;
              },
            ),
            const SizedBox(
              height: 10,
            ),
            ElevatedButton(onPressed: (){
              **print(context.ref(screenProvider));**
              context.go('/page2');},
                child: const Text(
                  'Submit',
                style: TextStyle(
                  color: Colors.white,
                ),
                ),
            )
          ],
        ),

      ),
    );

  }
}



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?