Skip to main content

Overview

Node is the fundamental building block of an AppFlowy Editor document. Each node has a type (e.g., “paragraph”, “heading”), attributes (data), and can contain child nodes, forming a tree structure. Nodes are organized in a linked list structure for efficient traversal and manipulation.

Constructor

Node({
  required String type,
  String? id,
  Node? parent,
  Attributes attributes = const {},
  Iterable<Node> children = const [],
})

Named Constructors

// Create from JSON
Node.fromJson(Map<String, Object> json)

Parameters

type
String
required
The type of the node (e.g., “paragraph”, “heading”, “bulletedList”).
id
String
Unique identifier for the node. Auto-generated if not provided.
parent
Node
Parent node in the tree.
attributes
Attributes
default:"{}"
Node data/attributes (e.g., delta for text, level for headings).
children
Iterable<Node>
default:"[]"
Child nodes.

Properties

type
String
The type identifier of this node.
id
String
Unique identifier for the node.
parent
Node?
Parent node, or null if this is the root.
children
List<Node>
List of child nodes.
attributes
Attributes
Copy of the node’s attributes.
path
Path
The path from the root to this node (e.g., [0], [1, 2]).
delta
Delta?
The text delta if this node contains text, otherwise null.
key
GlobalKey
Flutter global key for this node’s widget.
Layer link for overlay positioning.
externalValues
NodeExternalValues?
External values for custom node data.
extraInfos
Map?
Temporary data cleared after rendering.

Methods

updateAttributes

void updateAttributes(Attributes attributes)
Updates the node’s attributes by merging with existing attributes.
attributes
Attributes
required
Attributes to merge into the node.
Example:
node.updateAttributes({'bold': true, 'level': 2});

insert

void insert(Node entry, {int? index})
Inserts a child node at the specified index.
entry
Node
required
The node to insert.
index
int
The index to insert at. Defaults to end of children.

insertBefore

void insertBefore(Node entry)
Inserts a node before this node in the parent’s children.

insertAfter

void insertAfter(Node entry)
Inserts a node after this node in the parent’s children.
bool unlink()
Removes this node from its parent’s children. Returns: true if successful, false if the node has no parent.

childAtPath

Node? childAtPath(Path path)
Returns the descendant node at the relative path.
path
Path
required
Relative path from this node.
Returns: The node at the path, or null. Example:
final child = node.childAtPath([0]); // First child
final grandchild = node.childAtPath([0, 1]); // Second child of first child

childAtIndexOrNull

Node? childAtIndexOrNull(int index)
Returns the child at the given index, or null if out of bounds.

copyWith

Node copyWith({
  String? type,
  Iterable<Node>? children,
  Attributes? attributes,
})
Creates a deep copy of the node with optional overrides. Returns: A new node with a new ID.

deepCopy

Node deepCopy()
Creates a deep copy of the node including all children.

toJson

Map<String, Object> toJson()
Serializes the node to JSON format. Example:
final json = node.toJson();
// {
//   'type': 'paragraph',
//   'data': {'delta': [...]},
//   'children': [...]
// }

notify

void notify()
Manually triggers a rebuild of this node’s widget.

checkDocumentIntegrity

void checkDocumentIntegrity()
Validates document tree integrity (for debugging).

Example

import 'package:appflowy_editor/appflowy_editor.dart';

// Create a paragraph node
final paragraph = Node(
  type: 'paragraph',
  attributes: {
    'delta': Delta([TextInsert('Hello, World!')]).toJson(),
  },
);

// Create a heading node with children
final heading = Node(
  type: 'heading',
  attributes: {
    'level': 1,
    'delta': Delta([TextInsert('Title')]).toJson(),
  },
  children: [paragraph],
);

// Access properties
print(heading.type); // 'heading'
print(heading.path); // [0] (if at root)
print(heading.children.length); // 1

// Get the delta
final delta = heading.delta;
if (delta != null) {
  print(delta.toPlainText()); // 'Title'
}

// Update attributes
heading.updateAttributes({'level': 2});

// Navigate children
final firstChild = heading.childAtIndexOrNull(0);
final nodeAtPath = heading.childAtPath([0]);

// Copy the node
final copy = heading.copyWith();

// Serialize to JSON
final json = heading.toJson();
final fromJson = Node.fromJson(json);

Common Node Types

  • paragraph - Text paragraph
  • heading - Heading with level 1-6
  • bulletedList - Bulleted list item
  • numberedList - Numbered list item
  • todoList - Todo list item
  • quote - Block quote
  • code - Code block
  • divider - Horizontal divider
  • image - Image block
  • page - Root page node

See Also

Build docs developers (and LLMs) love