andyduke / styled_text_package

Text widget with formatted text using tags. Makes it easier to use formatted text in multilingual applications.
https://pub.dev/packages/styled_text
BSD 3-Clause "New" or "Revised" License
74 stars 48 forks source link

Building after dispose when rolling a list causing exceptions #55

Closed igorcd closed 2 years ago

igorcd commented 2 years ago

Hello, first thanks for that magnific package. My project is throwing the following exception when rolling a ListView (Probabily is building after element hide from list).

E/flutter (15254): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter (15254): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.

line: package:styled_text/styled_text.dart:341

image

Thanks

andyduke commented 2 years ago

@igorcd Could you provide a minimal code to reproduce the problem?

igorcd commented 2 years ago

Example:

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var controller = ScrollController();

  void navigateToBottom() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      controller.jumpTo(controller.position.maxScrollExtent);
    });
  }

  @override
  void initState() {
    super.initState();
    navigateToBottom();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView.separated(
        padding: const EdgeInsets.all(20),
        controller: controller,
        itemCount: 500,
        separatorBuilder: (context, index) => const SizedBox(height: 20),
        itemBuilder: ((context, index) {
          return Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: Colors.grey.shade100,
              border: Border.all(color: Colors.grey.shade200),
              borderRadius: BorderRadius.circular(8),
            ),
            child: StyledText(
              text: 'Test: <bold>bold</bold> and <red>red color</red> text.',
              tags: {
                'bold': StyledTextTag(style: const TextStyle(fontWeight: FontWeight.bold)),
                'red': StyledTextTag(style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.red)),
              },
            ),
          );
        }),
      ),
    );
  }
}
andyduke commented 2 years ago

@igorcd Please check now with version 5.1.0.

igorcd commented 2 years ago

@andyduke resolved in 5.1.0 Thanks for you help.