1

I'm getting this error when I click on a Video Card. The video with details will be moved to the mini-player. The Error shows Provider.of<dynamic>, but I'm not using Provider.of<dynamic>, instead I'm using StateProvider<dynamic> I've tried with a solution Consumer<TaskData>, which is not working,as it says The type 'Consumer' is declared with 0 type parameters.The possible solutions and some explanation about the Provider Error will be a huge help..

-------------------------------------------------------------

Error

The following assertion was thrown while handling a gesture:
Tried to call Provider.of<dynamic>. This is likely a mistake and is therefore
unsupported.

If you want to expose a variable that can be anything, consider changing
`dynamic` to `Object` instead.
'package:provider/src/provider.dart':
Failed assertion: line 330 pos 7: 'T != dynamic'

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.22000.318], locale en-PK)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.0.5)
[!] Android Studio (not installed)
[√] VS Code (version 1.65.2)
[√] Connected device (3 available)
[√] HTTP Host Availability

! Doctor found issues in 1 category.

Pubspec.yaml

name: yt_project
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.16.0 <3.0.0"


dependencies:
  flutter:
    sdk: flutter
  flutter_svg: ^1.0.3
  flutter_riverpod: ^1.0.3
  miniplayer: ^1.0.1
  timeago: ^3.2.2

  cupertino_icons: ^1.0.2
  provider: ^6.0.2
  riverpod: ^1.0.3

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^1.0.0


flutter:

  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/

dart file where the StateProvider and Consumer widget is used

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yt_project/data.dart';
import 'package:yt_project/main/LandingPage/Bar_Screens/home_screen.dart';

//Key To Mange Our State
final selectedVideoProvider = StateProvider<Video?>((ref) => null);

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

  @override
  State<BottomNavBar> createState() => _BottomNavBarState();
}

class _BottomNavBarState extends State<BottomNavBar> {
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Consumer(
        builder: (context, watch, _) {
          final selectedvideo = watch.watch(selectedVideoProvider.state).state;
          if (kDebugMode) {
            print(selectedvideo);
          }
          return Stack(
            children: _screens
                .asMap()
                .map(
                  (i, screen) => MapEntry(
                    i,
                    Offstage(
                      child: screen,
                      offstage: _selectedindex != i,
                    ),
                  ),
                )
                .values
                .toList(),
          );
        },
      ),
}
}

Vide_card.dart file

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yt_project/data.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:yt_project/main/LandingPage/Colors/colors.dart';

class VideoCard extends StatelessWidget {
  final Video video;
  const VideoCard({Key? key, required this.video}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        context.read().state = video;
      },
      child: Column(
        children: [
          Stack(
            children: [
              Image.network(
                video.thumbnailUrl,
                height: 220,
                width: double.infinity,
                fit: BoxFit.cover,
              ),
              Positioned(
                top: 10.0,
                right: 8.0,
                child: Container(
                  decoration: const BoxDecoration(
                    borderRadius: BorderRadius.all(
                      Radius.circular(3.0),
                    ),
                    color: Color.fromARGB(255, 90, 89, 89),
                  ),
                  height: 32.0,
                  child: Padding(
                    padding: const EdgeInsets.only(
                      top: 4.0,
                      bottom: 4.0,
                      right: 6.0,
                      left: 6.0,
                    ),
                    child: Row(
                      children: [
                        const Icon(
                          Icons.volume_off_outlined,
                        ),
                        const SizedBox(
                          width: 5,
                        ),
                        Container(
                          height: 12.0,
                          width: 1.5,
                          color: dividerColor,
                        ),
                        const SizedBox(
                          width: 5,
                        ),
                        const Icon(
                          Icons.closed_caption,
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: 8.0,
                right: 8.0,
                child: Container(
                  decoration: const BoxDecoration(
                    borderRadius: BorderRadius.all(
                      Radius.circular(3.0),
                    ),
                    color: Color.fromARGB(255, 53, 51, 51),
                  ),
                  height: 20.0,
                  child: Padding(
                    padding: const EdgeInsets.only(
                      top: 0.5,
                      bottom: 0.5,
                      right: 4.0,
                      left: 4.0,
                    ),
                    child: Text(
                      video.duration,
                      style: Theme.of(context).textTheme.caption!.copyWith(
                            color: Colors.white,
                            fontWeight: FontWeight.w600,
                          ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(
              top: 8.0,
              bottom: 14.0,
              right: 8.0,
              left: 8.0,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.only(right: 8.0),
                  child: GestureDetector(
                    onTap: () {},
                    child: CircleAvatar(
                      foregroundImage: NetworkImage(
                        video.author.profileImageUrl,
                      ),
                    ),
                  ),
                ),
                const SizedBox(
                  width: 5.0,
                ),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Wrap(
                        children: [
                          Text(
                            video.title,
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            style:
                                Theme.of(context).textTheme.bodyText1!.copyWith(
                                      fontSize: 15.0,
                                    ),
                          ),
                        ],
                      ),
                      Wrap(
                        children: [
                          Text(
                            '${video.author.username} \u2022 ${video.viewCount} views \u2022 ${timeago.format(
                              video.timestamp,
                            )}',
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            style:
                                Theme.of(context).textTheme.caption!.copyWith(
                                      fontSize: 14.0,
                                    ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                const SizedBox(
                  width: 10,
                ),
                GestureDetector(
                  child: const Icon(
                    Icons.more_vert,
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
M Nouman
  • 437
  • 1
  • 5
  • 22
  • The `GestureDetector` `onTap` method in `VideoCard` widget looks suspicious on line `context.read().state = video;`. I think that you need to pass type of object (class) to `context.read`, something like: `context.read().state = video;` where `ClassName` should be the class you are reading your state from. – Ante Bule Mar 25 '22 at 13:07
  • In the tutorial it like `context.read(selectedVideoProvider).state = video;`, but now `context.read()`, does not take any parameter. Btw suggest any class name that might solve the issue. – M Nouman Mar 25 '22 at 13:10
  • @AnteBule is right, you never provided the Type of Provider whose data you want to read, which is why it's assuming it `dynamic` and cannot read the method. You've to type cast it as `context.read()....` – Lalit Fauzdar Mar 25 '22 at 13:47
  • I understand the concept that I have to provide type, but the thing that i'm not able to understand is that what is that type that I have to provide? if it's a class,then the only class that i'm able to see is the ` StatefulWidget's Class BottomNavBar`. Can you please explain which or what type of class will be the `Notifier` , in `read()`. – M Nouman Mar 27 '22 at 14:33

0 Answers0