0

Im new in development, so Im having trouble with my screenutilinit function. I have added the dependencies ' flutter_screenutil: ^4.0.2+3 ' in the pubspec.yaml file but it has error saying 'The method 'ScreenUtilInit' isn't defined for the class 'MyApp'.

  • 'MyApp' is from 'package:sparks/main.dart' ('lib/main.dart').'

Last time the code works (fine) for my design but since I migrated my project files into new folder, it has the red underline beneath it. Can anyone tell me why?

additional note: I migrated the project files into new folder because I was having trouble with my firebase set up, thus I decided to create a new project and copy and paste the lib files into the new project.

here is my main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sparks/screens/Welcomecomponent/welcome.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: Size(414, 896), allowFontScaling: false,
        child: MaterialApp(
          title: 'Spark',
          theme: ThemeData(
            //primarySwatch: Colors.white,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: WelcomeScreen(),
        ));
  }
}

this is my pubspec.yaml file:

name: sparks
description: A new Flutter project.

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.0
  flutter_screenutil: ^4.0.2+3


dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

  assets:
    - assets/images/
    - assets/icons/
    - assets/fonts/

here is the error: enter image description here

4
  • Tried running flutter pub get? Feb 9, 2021 at 8:41
  • already did :((
    – Sarah ED
    Feb 9, 2021 at 8:44
  • 1
    Try flutter clean and also try to run the project if this didn't help quit editor and reopen. Also add screenshot of the error you are seeing in screen. stackoverflow.com/questions/55771635/… Feb 9, 2021 at 8:49
  • Did what you asked and the suggestions in your link but it still didn't work. I've added the error screenshot
    – Sarah ED
    Feb 9, 2021 at 9:07

1 Answer 1

1

The Widget you are trying to use needs a parameter called builder(). and ScreenUtilInit doesn't accept child: parameter.

Try this:

ScreenUtilInit(
  designSize: Size(360, 690),
  allowFontScaling: false,
  builder: () => MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Flutter_ScreenUtil',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: HomePage(),
  ),
);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.