简体   繁体   中英

Android studio doesn't recognize flutter class AppBar?

How do I fix it const issue on appBar?

图片

While you are adding title:Text(..) AppBar is not const anymore. Therefore, the parent MaterialApp cant be const .

You can do

@override
Widget build(BuildContext context) {
  return MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Scaffold(
      appBar: AppBar(
        title: const Text("this is te"),
      ),
    ),
  );
}

Your code will have diagnostic-messages on error: const_with_non_const

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

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(),
    ),
  );
}
// fail
import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
      ),
    ),
  );
}

While Text('AppBar Demo') is a const, it is preferred to add const before it.

// not preferred
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('AppBar Demo'),
        ),
      ),
    ),
  );
}
// preferred
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('AppBar Demo'),
        ),
      ),
    ),
  );
}

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