Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to present an empty view in flutter?

Tags:

flutter

How to present an empty view in flutter as Widget.build cannot return null to indicate that there is nothing to render.

like image 570
lext Avatar asked Nov 24 '18 05:11

lext


People also ask

How do you empty an object in Flutter?

The first way, assign the empty data with [] syntax. This creates an empty array list without elements. All the above are generic typed with dynamic values. The third way, If you want to create a typed empty number list, You have to assign with generic with int.


2 Answers

For anyone like me who was wondering what is "the correct way" to show an empty widget - official Material codebase uses this:

Widget build(BuildContext context) {   return SizedBox.shrink(); } 

SizedBox.shrink() is a widget that is unlike Container or Material has no background or any decorations whatsoever. It sizes itself to the smallest area possible, if not influenced by parent constraints.

like image 140
George Avatar answered Sep 22 '22 20:09

George


import 'package:flutter/material.dart';  void main() => runApp(MyApp());  class MyApp extends StatelessWidget {   // This widget is the root of your application.   @override   Widget build(BuildContext context) {     return MaterialApp(       title: 'Flutter Demo',       theme: ThemeData(         primarySwatch: Colors.blue,       ),       home: MyHomePage(),     );   } }  class MyHomePage extends StatefulWidget {   @override   _MyHomePageState createState() => _MyHomePageState(); }  class _MyHomePageState extends State<MyHomePage> {   @override   Widget build(BuildContext context) {     return Scaffold(      );   } } 

You can also simply return an empty Container and avoid using the Scaffold entirely. But this would result in a black screen if this is the only primary widget in you app, you can set the color property of the Container if you want to prevent the black background.

Example:

class _MyHomePageState extends State<MyHomePage> {   @override   Widget build(BuildContext context) {     return Container(       color: Colors.white // This is optional     );   } } 
like image 35
Rahul Mahadik Avatar answered Sep 21 '22 20:09

Rahul Mahadik