Lin
Lin

Reputation: 19

Flutter error- _InheritedProviderScope<Provider>(value: Instance of 'Provider', listening to value)

I am currently working with an app in Flutter and I am using Provider for state management. I am using ChangeNotifierProvider of . AvProvider is a Class as class AvProvider extends ChangeNotifier to open a time picker to select time using Methods and and then using notifyListeners(); to update the and display the selected time as text in the App.

While trying to implement this I am getting the following error on the emulator screen:

enter image description here

_InheritedProviderScope(value: Instance of 'Provider', listening to value).

How to get the selected time displayed just below the timer icon?

Here is my model class.

import 'package:flutter/material.dart';
class AvProvider with ChangeNotifier {
  var newTime;
  AvProvider (this.newTime);
  TimeOfDay _time = TimeOfDay(hour: 7, minute: 15);
  dynamic selectTime(context) async {
    final TimeOfDay newTime = await showTimePicker(
      context: context,
      initialTime: _time,
    );
    if (newTime != null) {
      _time = newTime;
    }
    else{
      print("Time is not selected");
    }
    print(newTime);
    return newTime;
  }

  String get getDisplayText => newTime.toString();
  notifyListeners();
}        

And this is part of my code for the screen.


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:trial/functionality/avProvider.dart';

class AvalPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<AvProvider>(
      create: (context) => AvProvider(context),
      child: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints viewportConstraints) {
            return SingleChildScrollView(
              physics: AlwaysScrollableScrollPhysics(),
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  minHeight: viewportConstraints.maxHeight,
                ),
                child: Column(
                  children: <Widget>[
                    Container(
                      color: Color(0xFFFFFF),
                      child: Column(

                        children: <Widget>[
                          SizedBox(height: 5),
                          Card(
                            child: InkWell(
                              splashColor: Colors.blue.withAlpha(40),
                              onTap: () {},
                              child: Padding(
                                padding: const EdgeInsets.all(2.0),
                                child: Container(
                                  decoration: BoxDecoration(
                                    gradient: LinearGradient(
                                      colors: [
                                        Colors.redAccent,
                                        Colors.tealAccent,
                                      ],
                                    ),
                                    borderRadius:
                                    BorderRadius.circular(10),),
                                  height: 150,
                                  child: Padding(
                                    padding:
                                    const EdgeInsets.all(0.5),
                                    child: Container(
                                      decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.circular(
                                            15), // initially 35
                                      ),
                                      child: Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                        mainAxisAlignment: MainAxisAlignment
                                            .center,
                                        children: [
                                          Container(
                                            child: Row(
                                                mainAxisAlignment:
                                                MainAxisAlignment.spaceEvenly,
                                                children: <Widget>[
                                                  Text(
                                                    'MONDAY',
                                                    style: TextStyle(
                                                      color: Colors.teal
                                                          .shade900,
                                                      fontFamily: 'Noto_Sans',
                                                      fontSize: 17.0,
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    width: 20.0,
                                                  ),
                                                  Icon(
                                                    Icons.timer_outlined,
                                                    size: 45,
                                                    color: Color(0xff40C9B9),
                                                  ),
                                                  Row(
                                                    mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceAround,
                                                    children: <Widget>[
                                                      Text(
                                                        'Start',
                                                        style: TextStyle(
                                                          color:
                                                          Colors.teal.shade900,
                                                          fontFamily: 'Noto_Sans',
                                                          fontSize: 15.0,
                                                        ),
                                                      ),
                                                      //SizedBox(height: 5),
                                                      SizedBox(
                                                        width: 50.0,
                                                      ),
                                                      ElevatedButton(
                                                        child: Icon(
                                                          Icons.timer_outlined,
                                                          size: 45,
                                                          color: Color(
                                                              0xff40C9B9),
                                                        ),
                                                        onPressed: () {
                                                          Provider.of<
                                                              AvProvider>(
                                                              context,
                                                              listen: false)
                                                              .selectTime(
                                                              context);
                                                        },
                                                      ),

                                                      Text(
                                                        'End',
                                                        style: TextStyle(
                                                          color:
                                                          Colors.teal.shade900,
                                                          fontFamily: 'Noto_Sans',
                                                          fontSize: 15.0,
                                                        ),
                                                      ),
                                                      //SizedBox(height:5),
                                                    ],
                                                  ),
                                                ]),
                                          ),
                                          Container(
                                            child: Row(
                                                mainAxisAlignment:
                                                MainAxisAlignment.start,
                                                children: <Widget>[
                                                  SizedBox(
                                                    width: 10.0,
                                                  ),
                                                  SizedBox(
                                                    width: 50.0,
                                                  ),
                                                  // SizedBox(height: 5),
                                                  Expanded(
                                                    child: Consumer<
                                                        AvProvider>( //                    <--- Consumer
                                                      builder: (context,
                                                          avProvider,
                                                          _) {
                                                        return Provider
                                                            .of<AvProvider>(
                                                            context)
                                                            .getDisplayText ==
                                                            null
                                                            ? Center(
                                                            child: CircularProgressIndicator())
                                                            :
                                                        Text(Provider
                                                            .of<AvProvider>(
                                                            context)
                                                            .getDisplayText,
                                                            overflow: TextOverflow
                                                                .ellipsis);
                                                        //Text('${Provider.of<AvProvider>(context).getDisplayText}', overflow: TextOverflow.ellipsis);
                                                      },
                                                    ),
                                                  ),

                                                ]),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),

                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          }),
    );
  }
}

In my code, I have kept two time pickers for start time and end time. Here I have placed provider and consumer only for the END time time picker.

Upvotes: 1

Views: 1238

Answers (1)

Muhammet
Muhammet

Reputation: 89

In your main.dart file connect your provider to application with

ChangeNotifierProvider.value(
  value: AvProvider(),
  child: MaterialApp(home: //some widget)
)

Upvotes: 0

Related Questions