Search code examples
fluttererror-handlingscroll

SingleChildScrollView not working in ErrorWidget.builder


I would like to show all the errors raised to the user.

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    builder: (context, widget) {
      ErrorWidget.builder = (errorDetails) {
        Widget error = Text(errorDetails.toString());
        if (widget is Scaffold || widget is Navigator) {
          error = SingleChildScrollView(child: error); // <= not scrolling
        }
        return error;
      };
      if (widget != null) return widget;
      throw StateError('widget is null');
    },
    home: const SafeArea(child: Scaffold(body: MyHomePage())),
  ));
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    throw "build exception";
  }
}

But the widget is not scrolling.

I tried to wrap SingleChildScrollView in Scaffold, but it doesn't work.

What should I do to make it scrollable?


Solution

  • Replace the condition in ErrorWidget.builder

    ErrorWidget.builder = (errorDetails) {
          Widget error = Text(errorDetails.toString());
          if (widget is FocusScope || widget is Navigator) {
            error = SingleChildScrollView(child: error);
          }
          return error;
        };
    

    Here Scaffold is replaced with FocusScope because MyHomePage is returning a widget of type FocusScope. Hope it helps.