Home > Mobile >  Android studio doesn't recognize flutter class AppBar?
Android studio doesn't recognize flutter class AppBar?

Time:04-10

enter image description here

How do i fix it ?

CodePudding user response:

can you hover the mouse pointer over the red line and tell me what the IDE tell you ?

CodePudding user response:

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

CodePudding user response:

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