Android studio doesn't recognize flutter class AppBar?

Viewed 95

How do I fix it const issue on appBar ?

image

2 Answers

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'),
        ),
      ),
    ),
  );
}
Related