Home > Net >  Could not find the correct Provider<T> above this ResultForm Widget in Flutter
Could not find the correct Provider<T> above this ResultForm Widget in Flutter

Time:02-19

I`m using provider in my Flutter app. And here is my file, where I use this provider:

class WorkScreen extends StatelessWidget {
  WorkScreen({Key? key}) : super(key: key);

  final String title = 'Service Station';
  final List<Work> works = [Work('Repair Engine', 200, 0.5), Work('Repair door', 250, 1.5), Work('Repair wheels', 120, 2)];

@override
  Widget build(BuildContext context) {

    return ChangeNotifierProvider(
      create: (context) => Cart(),
      child: Scaffold(
      appBar: AppBar(
        title: Text(title),
        actions: [
          IconButton(
              onPressed: _onSettingsPressed,
              icon: const Icon(Icons.settings))
        ],
      ),
      body: WorkList(works),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => ResultForm()));
        },
        child: const Icon(Icons.calculate)
      ),
    )
    );
  }
}

When I press floating action button the result form is appears. And here is this file:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:service_station/screens/work_tile.dart';
import 'package:service_station/models/work_model.dart';
import 'package:provider/provider.dart';

class ResultForm extends StatelessWidget {

  String countItemsText = ' points in your cart';

  @override
  Widget build(BuildContext context) {

    var cart = context.watch<Cart>();

    return Scaffold(
      appBar: AppBar(title: Text('Result')),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(15, 15, 0, 0),
        child: Column(
          children: [
            Text(countItemsText),
          ListView.separated(
            itemCount: cart.works.length,
            itemBuilder: (context, index) {
              return WorkTile(cart.works[index]);
            },
            separatorBuilder: (context, index) {
              return const Divider();
            },
          ),
          ],
        ),
      )
    );
  }
}

And when I click on this button, the app gives an error:

Error: Could not find the correct Provider<Cart> above this ResultForm Widget

Can you help me with this problem? If you need other files, write in comment.

CodePudding user response:

You need to use ChangeNotifierProvider.value to pass your ChangeNotifier (Cart) to the other page:

class WorkScreen extends StatelessWidget {
  WorkScreen({Key? key}) : super(key: key);

  final String title = 'Service Station';
  final List<Work> works = [Work('Repair Engine', 200, 0.5), Work('Repair door', 250, 1.5), Work('Repair wheels', 120, 2)];
  final Cart cartNotifier = Cart();

  @override
  Widget build(BuildContext context) {

    return ChangeNotifierProvider(
      create: (context) => cartNotifier,
      child: Scaffold(
      appBar: AppBar(
        title: Text(title),
        actions: [
          IconButton(
              onPressed: _onSettingsPressed,
              icon: const Icon(Icons.settings))
        ],
      ),
      body: WorkList(works),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).push(
              MaterialPageRoute(
                  builder: (context) => ChangeNotifierProvider<Cart>.value(
                      value: cartNotifier, 
                      child: ResultForm()
                  )
              )
          );
        },
        child: const Icon(Icons.calculate)
      ),
    )
    );
  }
}
  • Related