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
#Create a dedicated public, statically accessible, and explicitly defined constructor, top-level function, or class member for use as a preview:
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.