Home > database >  Cannot get the flutter_markdown pub.dev to work as a widget
Cannot get the flutter_markdown pub.dev to work as a widget

Time:05-16

I want to create a widget that displays Markdown.

I am using the following reference code from here (https://pub.dev/packages/flutter_markdown/example) :

/// void main() {
///   runApp(
///     MaterialApp(
///       title: "Markdown Demo",
///       home: Scaffold(
///         appBar: AppBar(
///           title: const Text('Simple Markdown Demo'),
///         ),
///         body: SafeArea(
///           child: Markdown(
///             data: _markdownData,
///           ),
///         ),
///       ),
///     ),
///   );
/// }

This line generates an error: Markdown(data: markdownData);

This is my code:

import 'package:flutter_markdown/flutter_markdown.dart';

class MarkdownCustomWidget extends StatefulWidget {
  const MarkdownCustomWidget({Key{NULL_SAFE_QUESTION_SENTINEL} key , this.width,this.height,this.markdownData, }) : super(key: key);

  final double{NULL_SAFE_QUESTION_SENTINEL} width;
    final double{NULL_SAFE_QUESTION_SENTINEL} height;
    final String{NULL_SAFE_QUESTION_SENTINEL} markdownData;

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

class _MarkdownCustomWidgetState extends State<MarkdownCustomWidget>  {
  
  
  @override
  Widget build(BuildContext context) {
    
    return Container(
        Markdown(data: markdownData); 
    );
  }
}

CodePudding user response:

You can't directly put a widget inside any other widget You have to put it inside Parent widget's child parameter/builder/children[]

Container has child parameter so define markdown widget as a child of Container.

Like this:

Container(child: Markdown(data: markdownData))

If you don't specify width and height for Markdown, it will throw Infinite Size error and it won't show anything on the screen. To fix this:

Wrap Markdown inside a Flexible widget:

Flexible(child: Container(child: Markdown(data: markdownData))),
  • Related