Code Monkey home page Code Monkey logo

Comments (6)

tehsunnliu avatar tehsunnliu commented on July 21, 2024

It seems for some reason this._containerKey.currentContext is null in captureAsUiImage(...). Anyways I used RepaintBoundary instead and got it working.

from screenshot.

ritheshSalyan avatar ritheshSalyan commented on July 21, 2024

@tehsunnliu Can I get the minimum code to reproduce this issue? Did you wrap your widget with Screenshot Widget?

from screenshot.

tehsunnliu avatar tehsunnliu commented on July 21, 2024

@ritheshSalyan Hi, Thank you for your response. First of all my apologies. I should have tested my code more before opening an issue. While I was writing a sample code to reproduce the error I mentioned. I found that I was calling setState() before calling screenshotController.capture(...) which called build() function. And in build function, I've added _screenshotControllers.clear(); which clears all the ScreenshotController in that list. Hence, throwing NoSuchMethodError.

However, this same implementation works fine with RepaintBoundary. I'm not sure how this doesn't affect RepaintBoundary.

Anyways below is the code which throws NoSuchMethodError.

import 'package:flutter/material.dart';
import 'package:screenshot/screenshot.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _isSaving = false;
  List<ScreenshotController> _screenshotControllers = [];
  List<Widget> _widgets = [
    Card(
      child: ListTile(
        leading: Icon(
          Icons.person,
        ),
        title: Text('Testing Screenshot 1'),
      ),
    ),
    Card(
      child: ListTile(
        leading: Icon(
          Icons.person,
        ),
        title: Text('Testing Screenshot 2'),
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    _screenshotControllers.clear();

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: _widgets.map((e) {
            ScreenshotController controller = ScreenshotController();
            _screenshotControllers.add(controller);
            return Screenshot(
              controller: controller,
              child: e,
            );
          }).toList(),
        ),
      ),
      floatingActionButton: _isSaving
          ? CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(Colors.white),
            )
          : FloatingActionButton(
              onPressed: _saveWidgets,
              tooltip: 'Save',
              child: Icon(Icons.save),
            ),
    );
  }

  void _saveWidgets() async {
    setState(() {
      _isSaving = true;
    });
    try {
      for (int i = _screenshotControllers.length - 1; i >= 0; --i) {
        var data =
            await _screenshotControllers[i].capture(pixelRatio: 3);
        print('ScreenshotData: $data');
      }
    } catch (e) {
      print('failed to capture: $e');
      debugPrintStack();
    }
    setState(() {
      _isSaving = false;
    });
  }
}

from screenshot.

ritheshSalyan avatar ritheshSalyan commented on July 21, 2024

@tehsunnliu The code you have shared is working perfectly fine, It didn't throw any error,
Here Is the video Proof for working.
https://user-images.githubusercontent.com/44966372/118475356-c913eb00-b729-11eb-9a55-f7d6207652f5.mp4

Can I know your flutter version?

You can close this issue if it is working.

from screenshot.

tehsunnliu avatar tehsunnliu commented on July 21, 2024

Oh, I don't know why it's happening with me with the same code I haven't changed anything. Have you tried on debug mode?
I'll share the Flutter version as soon as I get access to my pc.
Thank you.

from screenshot.

tehsunnliu avatar tehsunnliu commented on July 21, 2024
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.6, on Linux, locale en_IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Chrome - develop for the web
[✓] Android Studio
[✓] Connected device (1 available)

• No issues found!

from screenshot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.