Sarah ED
Sarah ED

Reputation: 75

ScreenUtilInit - flutter

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'.

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

Upvotes: 1

Views: 3868

Answers (1)

Mayur Chaudhary
Mayur Chaudhary

Reputation: 324

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(),
  ),
);

Upvotes: 3

Related Questions