0

I am trying to run sample Riverpod program from Getting started Riverpod page

Pubspec is specified as given on the page.

Debug Console dumps the following message:

Launching lib/main.dart on Chrome in debug mode...
lib/main.dart:1
: Error: Method not found: 'Error.throwWithStackTrace'.
 ../…/framework/provider_base.dart:985
Error.throwWithStackTrace(error, chain);
    ^^^^^^^^^^^^^^^^^^^
: Error: A non-null value must be returned since the return type 'Never' doesn't 
allow null.
../…/framework/provider_base.dart:979
Never _rethrowProviderError(Object error, StackTrace stackTrace) {
  ^
Failed to compile application.
Exited (sigterm)

Just to be sure, following is main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// We create a "provider", which will store a value (here "Hello world").
// By using a provider, this allows us to mock/override the value exposed.
final helloWorldProvider = Provider((_) => 'Hello world');

void main() {
  runApp(
    // For widgets to be able to read providers, we need to wrap the entire
    // application in a "ProviderScope" widget.
    // This is where the state of our providers will be stored.
    const ProviderScope(
      child: MyApp(),
    ),
      );
}

// Extend ConsumerWidget instead of StatelessWidget, which is exposed by Riverpod
class MyApp extends ConsumerWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final String value = ref.watch(helloWorldProvider);

    return MaterialApp(
      home: Scaffold(
    appBar: AppBar(title: const Text('Example')),
    body: Center(
      child: Text(value),
        ),
      ),
    );
  }
}

And pubspec is as follows:

name: simple_app
description: A new Flutter project.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"
  flutter: ">=2.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_riverpod: ^2.0.0-dev.4

  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^1.0.0

flutter:

  uses-material-design: true

Environment: Flutter (Channel stable, 2.5.3, on macOS 12.2.1 21D62 darwin-x64, locale en-US)

Tusshu
  • 1,664
  • 3
  • 16
  • 32

0 Answers0