1

Here is the error message from Android Studio:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building MyZippyApp(dirty):
'package:provider/src/provider.dart': Failed assertion: line 181 pos 16: 'create != null': is not true.

The relevant error-causing widget was: 
  MyZippyApp file:///Users/mgav/AndroidStudioProjects/streakers_journal_beta/lib/main.dart:5:23
When the exception was thrown, this was the stack: 
#2      new Provider (package:provider/src/provider.dart:181:16)
#3      MyZippyApp.build (package:streakers_journal_beta/main.dart:10:12)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4701:28)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)

It appears as soon as I wrap 'MaterialApp' in a 'Provider' widget (using package version provider: ^4.3.2 in pubspec.yaml > ran 'Pub get'), then try to run the app.

Here is the main.dart code before wrapping with the 'Provider' widget (works perfectly well):

import 'package:flutter/material.dart';
import 'package:streakers_journal_beta/screens/tasks_screen.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyZippyApp());

class MyZippyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:
          TasksScreen(), // matches "TasksScreen" class created on tasks_screen.dart
    );
  }
}

Here is the main.dart code after wrapping with the provider widget:

import 'package:flutter/material.dart';
import 'package:streakers_journal_beta/screens/tasks_screen.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyZippyApp());

class MyZippyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Provider(
      child: MaterialApp(
        home:
            TasksScreen(), // matches "TasksScreen" class created on tasks_screen.dart
      ),
    );
  }
}

And per Farhan Syah's answer below, here is the revised code that reflects his answer (and fixes the problem):

import 'package:flutter/material.dart';
import 'package:streakers_journal_beta/screens/tasks_screen.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyZippyApp());

class MyZippyApp extends StatelessWidget {
  final String testingText = 'Thank you Farhan Syah';

  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (context) => testingText,
      child: MaterialApp(
        home:
            TasksScreen(), // matches "TasksScreen" class created on tasks_screen.dart
      ),
    );
  }
}
1

1 Answer 1

2

Provider widget has a required create properties.

you should use 'create' to create an object which you want to provide.

return Provider(
      create: (context)=> ObjectToBeProvidedHere(),
      child: MaterialApp(
        home:
            TasksScreen(), // matches "TasksScreen" class created on tasks_screen.dart
      ),
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.