invalid_widget_preview_application
The '@Preview(...)' annotation can only be applied to public, statically accessible constructors and functions.
Description
#The analyzer produces this diagnostic when a @Preview(...)
annotation
is applied to an invalid widget preview target. Widget previews can only
be applied to public, statically accessible, explicitly defined
constructors and functions.
Examples
#The following code produces this diagnostic because _myPrivatePreview
is private:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
// Invalid application to private top-level function.
@Preview()
// ignore: unused_element
Widget _myPrivatePreview() => Text('Foo');
The following code produces this diagnostic because myExternalPreview
is external
:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
// Invalid application to an external function.
@Preview()
external Widget myExternalPreview();
The following code produces this diagnostic because PublicWidget._()
is
private:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
class PublicWidget extends StatelessWidget {
// Invalid application to a private constructor.
@Preview()
PublicWidget._();
@override
Widget build(BuildContext) => Text('Foo');
}
The following code produces this diagnostic because instancePreview
is
an instance method:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
class PublicWidget extends StatelessWidget {
// Invalid application to a instance member.
@Preview()
Widget instancePreview() => PublicWidget();
@override
Widget build(BuildContext context) => Text('Foo');
}
The following code produces this diagnostic because _PrivateWidget
is
private:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
// ignore: unused_element
class _PrivateWidget extends StatelessWidget {
// Invalid application to a constructor of a private class.
@Preview()
_PrivateWidget();
@override
Widget build(BuildContext context) => Text('Foo');
}
The following code produces this diagnostic because _PrivateWidget
is
private:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
// ignore: unused_element
class _PrivateWidget extends StatelessWidget {
// Invalid application to a static method of a private class.
@Preview()
Widget privateStatic() => _PrivateWidget();
@override
Widget build(BuildContext context) => Text('Foo');
}
The following code produces this diagnostic because AbstractWidget
is
an abstract
class:
import 'package:flutter/widgets.dart';
import 'package:flutter/widget_previews.dart';
abstract class AbstractWidget extends StatelessWidget {
// Invalid application to a constructor of an abstract class.
@Preview()
AbstractWidget();
@override
Widget build(BuildContext context) => Text('Foo');
}
Common fixes
#除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。