Skip to content

Error while modifying Widget Preview annotations #173710

@DanTup

Description

@DanTup

Steps to reproduce

Unfortunately I don't know exactly what I typed that triggered this because I didn't notice it had crashed initially, but I'll post a comment on this issue if I figure out the steps.

Edit: I repro'd it.. it was when I added a non-const wrapper function:

@Preview(name: 'Test Preview 4', wrapper: Directionality(textDirection: TextDirection.rtl, child: w))

cc @bkonyi

Expected results

No crash, preview process keeps running.

Actual results

The preview stopped updating and I noticed the an exception log was written and noted in the process output.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:flutter/widget_previews.dart';
import 'package:flutter_counter/my_app.dart';
import 'package:http/http.dart';

void main() {
  runApp(const MyApp9());
}

class MyHomePage extends StatefulWidget {
  @Preview(name: 'Test Preview 4', wrapper: Directionality(textDirection: TextDirection.rtl, child: w))
  const MyHomePage.preview({Key? key}) : this(key: key, title: 'Test Preview');

  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() {
    return _MyHomePageState();
  } //
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    //
    // get(Uri.parse('https://www.google.co.uk/'));
    setState(() {
      _counter++;
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('test');
    debugPrint('test');

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
              // style: Theme.of(context).textTheme.headlineMedium,
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                '$_counter',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

void f2(
    ({
      int myInt,
    }) r) {
  if (r case (:var myInt)) {
    print(myInt);
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs

command

flutter widget-preview start --machine --web-server

exception

_TypeError: Null check operator used on a null value

#0      _PreviewVisitor.visitAnnotation (package:flutter_tools/src/widget_preview/dependency_graph.dart:85:75)
#1      AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:366:50)
#2      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:13243:20)
#3      _AnnotatedNodeMixin._visitCommentAndAnnotations (package:analyzer/src/dart/ast/ast.dart:19496:17)
#4      AnnotatedNodeImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:165:5)
#5      ConstructorDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:4349:11)
#6      _PreviewVisitor._scopedVisitChildren (package:flutter_tools/src/widget_preview/dependency_graph.dart:130:10)
#7      _PreviewVisitor.visitConstructorDeclaration (package:flutter_tools/src/widget_preview/dependency_graph.dart:67:5)
#8      ConstructorDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:4345:15)
#9      NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:13243:20)
#10     ClassDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2945:13)
#11     RecursiveAstVisitor.visitClassDeclaration (package:analyzer/dart/ast/visitor.dart:940:10)
#12     ClassDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:2935:50)
#13     NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:13243:20)
#14     CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:3611:21)
#15     LibraryPreviewNode.findPreviews (package:flutter_tools/src/widget_preview/dependency_graph.dart:195:20)
#16     PreviewDetector._findPreviewFunctions (package:flutter_tools/src/widget_preview/preview_detector.dart:217:30)
<asynchronous suspension>
#17     PreviewDetector._fileAddedOrUpdated (package:flutter_tools/src/widget_preview/preview_detector.dart:143:56)
<asynchronous suspension>
#18     PreviewDetector._onFileSystemEvent.<anonymous closure> (package:flutter_tools/src/widget_preview/preview_detector.dart:122:9)
<asynchronous suspension>
#19     PreviewDetectorMutex.runGuarded (package:flutter_tools/src/widget_preview/utils.dart:100:14)
<asynchronous suspension>
#20     PreviewDetector._onFileSystemEvent (package:flutter_tools/src/widget_preview/preview_detector.dart:81:5)
<asynchronous suspension>

Flutter Doctor output

Doctor output
[!] Flutter (Channel [user-branch], 3.33.0-1.0.pre-1371, on Microsoft Windows [Version 10.0.26100.4652], locale en-GB) [196ms]
    ! Flutter version 3.33.0-1.0.pre-1371 on channel [user-branch] at D:\Dev\Google\Flutter\Flutter main
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/setup.
    ! Warning: `dart` on your path resolves to D:\Tools\Dart\Stable\bin\dart.exe, which is not inside your current Flutter SDK checkout at D:\Dev\Google\Flutter\Flutter main. Consider adding D:\Dev\Google\Flutter\Flutter main\bin to the front of your path.
    ! Upstream repository unknown source is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
    • Framework revision ac43d96b78 (16 hours ago), 2025-08-12 17:34:27 -0400
    • Engine revision b0c8612eda
    • Dart version 3.10.0 (build 3.10.0-88.0.dev)
    • DevTools version 2.49.0
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-lldb-debugging
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Windows Version (11 Pro 64-bit, 24H2, 2009) [3.7s]

[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [2.8s]
    • Android SDK at C:\Users\danny\AppData\Local\Android\sdk
    • Emulator version 35.4.9.0 (build_id 13025442) (CL:N/A)
    • Platform android-36, build-tools 35.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

[✓] Chrome - develop for the web [49ms]
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.10.5) [48ms]
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.10.35122.118
    • Windows 10 SDK version 10.0.26100.0

[✓] Connected device (3 available) [402ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.26100.4652]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 139.0.7258.67
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 138.0.3351.77

[✓] Network resources [363ms]
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: widget previewsIssues related to the Widget Previewer toolingteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions