Skip to content

[camera] The example code from the README.md doesn't work #76537

@mvanbeusekom

Description

@mvanbeusekom

The example code listed in the README.md of the camera plugin doesn't work if you copy it "as-is" into a Flutter application. When running the App it will generate the following exception:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building CameraPreview:
No Directionality widget found.

Stack widgets require a Directionality widget ancestor to resolve the 'alignment' argument.

The default value for 'alignment' is AlignmentDirectional.topStart, which requires a text direction.

The specific widget that could not find a Directionality ancestor was: Stack
    alignment: AlignmentDirectional.topStart
    fit: expand
    dirty
The ownership chain for the affected widget is: "Stack ← AspectRatio ← CameraPreview ← AspectRatio ← CameraApp ← [root]"

Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree. It determines the ambient reading direction and is used, for example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
Instead of providing a Directionality widget, another solution would be passing a non-directional 'alignment', or an explicit 'textDirection', to the Stack.

The relevant error-causing widget was
CameraPreview
lib/main.dart:47
When the exception was thrown, this was the stack
#0      debugCheckHasDirectionality.<anonymous closure>
package:flutter/…/widgets/debug.dart:272
#1      debugCheckHasDirectionality
package:flutter/…/widgets/debug.dart:292
#2      Stack._debugCheckHasDirectionality
package:flutter/…/widgets/basic.dart:3562
#3      Stack.createRenderObject
package:flutter/…/widgets/basic.dart:3574
#4      RenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5347
...
════════════════════════════════════════════════════════════════════════════════
flutter doctor -v
[✓] Flutter (Channel beta, 1.26.0-17.5.pre, on macOS 11.2.1 20D74 darwin-x64, locale en-GB)
    • Flutter version 1.26.0-17.5.pre at /Users/maurits/fvm/versions/beta
    • Framework revision 1fe38dcb5f (12 days ago), 2021-02-10 16:25:47 -0800
    • Engine revision d4453f6018
    • Dart version 2.12.0 (build 2.12.0-259.9.beta)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/maurits/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /Users/maurits/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] IntelliJ IDEA Ultimate Edition (version 2020.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 53.1.5
    • Dart plugin version 203.6912

[✓] VS Code (version 1.53.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.19.0

[✓] Connected device (2 available)
    • Maurits van Beusekom’s iPhone (mobile) • 00008101-000D543E1A40001E • ios            • iOS 14.4
    • Chrome (web)                           • chrome                    • web-javascript • Google Chrome 88.0.4324.182

• No issues found!

Steps to Reproduce

  1. Run flutter create bug.
  2. Add camera dependency to your pubspec.yaml: camera: ^0.7.0+2
  3. Replace the contents of the main.dart file with the example code from the README.md;
  4. If on iOS add the camera permissions to the ios/Runner/Info.plist file:
<key>NSCameraUsageDescription</key>
<string>Can I use the camera please?</string>
  1. Run the App: flutter run

Expected results:

The example App shows a fullscreen camera preview.

Actual results:

The example App throws the following exception:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building CameraPreview:
No Directionality widget found.

Stack widgets require a Directionality widget ancestor to resolve the 'alignment' argument.

The default value for 'alignment' is AlignmentDirectional.topStart, which requires a text direction.

The specific widget that could not find a Directionality ancestor was: Stack
    alignment: AlignmentDirectional.topStart
    fit: expand
    dirty
The ownership chain for the affected widget is: "Stack ← AspectRatio ← CameraPreview ← AspectRatio ← CameraApp ← [root]"

Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree. It determines the ambient reading direction and is used, for example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
Instead of providing a Directionality widget, another solution would be passing a non-directional 'alignment', or an explicit 'textDirection', to the Stack.

The relevant error-causing widget was
CameraPreview
lib/main.dart:47
When the exception was thrown, this was the stack
#0      debugCheckHasDirectionality.<anonymous closure>
package:flutter/…/widgets/debug.dart:272
#1      debugCheckHasDirectionality
package:flutter/…/widgets/debug.dart:292
#2      Stack._debugCheckHasDirectionality
package:flutter/…/widgets/basic.dart:3562
#3      Stack.createRenderObject
package:flutter/…/widgets/basic.dart:3574
#4      RenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5347
...
════════════════════════════════════════════════════════════════════════════════

Metadata

Metadata

Assignees

No one assigned

    Labels

    p: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions