0

main.dart :

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

main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
      brightness: Brightness.light,
      primaryColor: Colors.amber[500],
      accentColor: Colors.blue,
      fontFamily: "Georgia",
      ),
      home: myscaffoldfunc()
    );
  }
}

Scaffold.dart :

import 'package:flutter/material.dart';
onpressedFun() => print("Hello");

myscaffoldfunc(){
  Scaffold(
        appBar: AppBar(
          title: Center(child: Text("Hello")),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: onpressedFun(),
          child: Icon(Icons.add),
          backgroundColor: Colors.amber,
          foregroundColor: Colors.white,
          hoverColor: Colors.amberAccent,
        ),
        body: Center(child: Text("Hello World")),
      );
}

Everything is working fine when I write the scaffold part in single file but as soon as I make a different file for scaffold it shows the below error in app. Any solution ?

And this error on debug console :-

    ═════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building Builder(dirty, dependencies: [MediaQuery]):
Null check operator used on a null value

The relevant error-causing widget was
MaterialApp
lib\main.dart:10
When the exception was thrown, this was the stack
#0      _MaterialAppState._materialBuilder
package:flutter/…/material/app.dart:818
#1      _WidgetsAppState.build.<anonymous closure>
package:flutter/…/widgets/app.dart:1545
#2      Builder.build
package:flutter/…/widgets/basic.dart:7798
#3      StatelessElement.build
package:flutter/…/widgets/framework.dart:4648
#4      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4574
...
════════════════════════════════════════════════════════════════════════════════
Reloaded 2 of 554 libraries in 551ms.

Now while running App:-

App

Avijit
  • 23
  • 3
  • If an answer helped you out, please consider accept it and mark it as a helpful answer to help out others, who encounter the same problem :) – Jahn E. Oct 12 '21 at 12:54

2 Answers2

0

myscaffoldfunc() is void, it doesn't return the Scaffold. Add a return keyword before Scafold.

HOWEVER, I strongly suggest you using Class instead of Functions for reusable widgets.

Please check this answer.

esentis
  • 4,190
  • 2
  • 12
  • 28
0

Your function has no return statement and thus returns null, which is throwing your null error. Moreover, you should not assign a function to the home parameter, a class would be better.

I would recommed this:

home: Myscaffoldfunc(),

In your other file:

 class Myscaffoldfunc extends StatelessWidget {
  final Widget child;

  const Myscaffoldfunc({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
      title: Center(child: Text("Hello")),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: onpressedFun(),
      child: Icon(Icons.add),
      backgroundColor: Colors.amber,
      foregroundColor: Colors.white,
      hoverColor: Colors.amberAccent,
    ),
    body: Center(child: Text("Hello World")),
  );
  }
}
Jahn E.
  • 1,228
  • 3
  • 6
  • 21