简体   繁体   中英

AppBar not working and considered as dead code

This is my code, it keeps showing up as "Dead Code." and it wont show anything in my AVD, can anyone help me with this?

import 'package:flutter/material.dart';

class SignInScreen extends StatelessWidget {
  const SignInScreen({Key? key}) : super(key: key);
  static String routeName = "/sign_in";

  @override
  Widget build(BuildContext context) {
    return Scaffold();
    AppBar();
  }
}

Firstly AppBar() should be inside the Scaffold widget.
The code should be something like this:

import 'package:flutter/material.dart';

class SignInScreen extends StatelessWidget {
  const SignInScreen({Key? key}) : super(key: key);
  static String routeName = "/sign_in";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar();
    );
  }
}

Secondly when you use return the code below the return statement doesn't execute, that is the reason your ide shows as a "dead code"

You are returning the Scaffold widget without adding AppBar inside Scaffold widget so it will never work. try this:

Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: Text("Your Navigationbar title"),
      ),
      body: Container(),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM